Pages

Subscribe:

Ads 468x60px

Sunday, December 4, 2022

how to upload file using python

 

How to upload file using python.

first of all create an html form using following code:

<html>
<body>
   <form enctype = "multipart/form-data" action = "python_upload.py" method = "post">
    
<p>Upload File: <input type = "file" name = "filenameupload" /></p>
 
    
<p><input type = "submit" value = "Upload" /></p>
 
</form>
</body>
</html>

creat a file with .py extention using following code:

import os
 
fileitem = form['filename_upload']
 
# check if the file has been uploaded
if fileitem.filename_upload:
    # strip the leading path from the file name
    fn = os.path.basename(fileitem.filename_upload)
     
   # open read and write the file into the server
    open(fn, 'wb').write(fileitem.file.read())

using following code you can upload file using python easily.

No comments:

Post a Comment