I'm a little bit confused, but directly opening file in request (however same is written in official requests guide) is not so "safe".
Just try:
import os
import requests
file_path = "/home/user_folder/somefile.txt"
files = {'somefile': open(file_path, 'rb')}
r = requests.post('http://httpbin.org/post', files=files)
Yes, all will be ok, but:
os.rename(file_path, file_path)
And you will get:
PermissionError:The process cannot access the file because it is being used by another process
Please, correct me if I'm not right, but it seems that file is still opened and I do not know any way to close it.
Instead of this I use:
import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
Now we do not get errors, so I recommend to use this way to upload multiple files or you could receive some errors.
Hope this answer well help somebody and save priceless time.