In this lecture, you learn how to upload a file via Python.
And for this, we will use as an example of this website, you can find the link in the resources of
this lecture.
Once you open the link, you'll see it down here.
There's a form and we can upload the file through this form, either using the browser as I'm doing
right now or by using Python.
So first, press the Choose File Bottom and then find the file.
And then press the press button to upload the file.
And you should see this final webpage, which sees that you have uploaded file successfully.
This is the contents of the file, so it was a text file in my case.
That's why we can see the content since it was text, if it was an MP, three, for example, or something
else.
You will see maybe some bytes in here.
So anyway, let's go back to the page where the upload form is and then go back to our Python environment
and create a new script and import requests.
So again, we will use requests just as we did previously when we downloaded the file.
So previously we use the get methods.
Now we will use the post methods.
And of course, first we need a new URL pointing to the posts you won't know.
What is the post URL?
Well, you can find the post URL of a form by doing an uploads again, so just pick a file, then press
the press button.
And once you do that, this you are earlier in the browser will change to something else.
So you see wait.
So once you see the file, upload the results, then you see the URL has changed, you should copy that
you URL, go to Python and put it inside quotes.
So you see, in my case, this URL ends with CGI.
Once you have that URL, then we're ready to make the request, which is the requests that post your
URL is the first parameter.
But we also need another parameter here, and that is the file we want to upload.
So for this, I'm just going to create a new file and use it to upload it to that website.
Just see my file that.
Hey, I'm a new file.
I'll get uploaded.
That's a file we want to upload a simple text file, and we want to provide that file in here, but
you don't want to provide the file path like that because this method expects the actual file object.
So you need to create a file object using file is able to open, then you provide the path to the file
you want to upload.
So my file that takes the open, it's in our modes or B.
It's even better because our B is both for binary and text files.
In our case, it would also work if we just left it out or since this is about text files, but our
B is more inclusive, so a file is the file object and then we provide a false argument, which is a
dictionary.
This dictionary has a key and a value.
The value is the file object we have created in here, and the key is, well, the key of that comes
from this form.
So what you want to do is first to reload this page.
Yes, then right click over the page and go to show page source or page souls or wherever.
It is in your browser and then you want to scroll down to find the form tag you see down here.
Otherwise, you can also use inspect elements, so click somewhere close to here and go to inspect elements.
And again, you'll find that form tag and then expand it and you want to find the input type tag.
This one in here and you want to see what value is attached to the name property.
In my case, it is up file.
So that is the place where we want to upload our file.
Therefore, in here we should see up file in your form.
It could be different, so make sure you have the correct value.
So that's code we'll post that file to this Web page.
Now all we are left to do is just print out.
The results of the requests, so request the text and let's run it.
I have a type of request like that we're on again.
And that's the results, so basically what we get here is the same page that we get when we upload the
file manually.
By pressing the red button.
So basically, we have this content displayed here.
You see, file upload the results, file upload the results here as well.
Here we have, of course, some estimate tags because this is a raw string to make sure that the file
was uploaded.
You should see here under the Files Contents Order.
And then we have this tag and that tag, and then we have the contents of the file.
Hey, I'm a new file.
I will get uploaded.
So that is the contents of the file we have uploaded, depending on the website.
This could be different.
So this web page shows us these results, but other web pages can show you something different.
But generally, this is how you upload files via Python.
So I hope that worked, and let's have some more videos suing the next one.