-1

I have a main folder with 20 subfolders that contains the images. I want to loop through them all and use the requests.post() to each images in subfolders. I have tried alot of methods but I can't seem to work. Any help would be appreciated

directory = '<directory>'
url ='http://127.0.0.1:5550/upload_image/0'
for files in os.listdir(directory):
    print(os.path.join(directory,files))
    requests.post(url)
  • Does this answer your question? [Python recursive folder read](https://stackoverflow.com/questions/2212643/python-recursive-folder-read) – mkrieger1 Aug 20 '20 at 17:13
  • No actually. It just tells me how to read the files in the folder but won't tell me how to use request.post in each one of them – BrightMango Aug 20 '20 at 17:21
  • Then your question is too broad. Is your problem how to find all files in a directory recursively or how to do a POST request? What exactly was the problem when you tried to do what you want? Please show a [mre] that demonstrates the issue. – mkrieger1 Aug 20 '20 at 17:25

1 Answers1

0

Use glob to loop through a directory.

from glob import glob
directory = 'directory'
for filename in glob(directory + '/*'): # add *jpg for all jpgs in directory
    print(filename)

Posting an image to a URL depends on how the server handles images. But you in your code you are not including the file in the request.

It will be something similar to:

requests.post(url , open(filename, 'rb'))
flicht
  • 91
  • 2
  • Hi. I used this but it doesn't work still. The api inside the server handles form data. Should I make any changes? – BrightMango Aug 20 '20 at 18:14