Upload files in Python Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will be looking into the process of file uploading in Python using cgi environment. One often comes across various web applications in which the client or the users is required to upload data in the form of a file(eg. image file, an audio file, text file, etc). There are two aspects to uploading a file, as there are two sides to that particular interaction being client-side and the server-side. A form needs to be created to accept the user input in the form of a file.An HTML form has different attributes that you can set it to, for instance, which URL is the uploaded data is to be submitted is done through the action attribute. An enctype attribute called multi-part/form-data is required in a HTML form to upload a file. Secondly we will be required to use the input tag of HTML and set it equal to "file". This adds a upload button in addition to an input button in the form. The below code example demonstrates it well: html <html> <body> <form enctype = "multipart/form-data" action = "python_script.py" method = "post"> <p>Upload File: <input type = "file" name = "filename" /></p> <p><input type = "submit" value = "Upload" /></p> </form> </body> </html> The output for the above HTML code would look like below: In the above code, the attribute action has a python script that gets executed when a file is uploaded by the user. On the server end as the python script accepts the uploaded data the field storage object retrieves the submitted name of the file from the form's "filename". Now all the server needs to do it is read the file that has been uploaded and write it to the "fileitem"(say, ). At the end of this entire process the uploaded file now will be written to the server.So, the python script looks somewhat like the below code: Python3 import os fileitem = form['filename'] # check if the file has been uploaded if fileitem.filename: # strip the leading path from the file name fn = os.path.basename(fileitem.filename) # open read and write the file into the server open(fn, 'wb').write(fileitem.file.read()) Note: The above python script doesn't work on every server as every server has its own dependencies to allow running a script in their server for security reasons. For instance if using an Azure server one would need to import msvcrt which is Microsoft visual C++ runtime module to work. Comment More infoAdvertise with us Next Article Upload files in Python R RajuKumar19 Follow Improve Article Tags : Python Write From Home Python-CGI Practice Tags : python Similar Reads Unzipping files in Python In this article we will see how to unzip the files in python we can achieve this functionality by using zipfile module in Python. What is a zip file ZIP file is a file format that is used for compressing multiple files together into a single file. It is used in an archive file format that supports l 3 min read Copy And Replace Files in Python In Python, copying and replacing files is a common task facilitated by modules like `shutil` and `os`. This process involves copying a source file to a destination location while potentially replacing any existing file with the same name. Leveraging functions like `shutil.copy2` and `os.remove`, thi 2 min read File Mode in Python In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Pytho 5 min read Writing to file in Python Writing to a file in Python means saving data generated by your program into a file on your system. This article will cover the how to write to files in Python in detail.Creating a FileCreating a file is the first step before writing data to it. In Python, we can create a file using the following th 4 min read How To Read .Data Files In Python? Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary w 4 min read Like