0

I have a simple query in python 3. I have a path as var where I am passing the file path from the directory. File out.txt contains a list of many files path

Example: out.txt

c:/python/text1.txt c:/python/text2.txt c:/python/text3.txt

with open(path, "rb") as sample:
payload = {

    'options': (None,json.dumps (options),'application/json'),'filename':(os.path.basename(path), sample,'application/octet-stream')
}


response = requests.request ("POST", url, headers=headers, files=payload, verify=False)

If will run like this >uploading c:/python/text1.txt...then after the operation it returns back uploading next file

2 Answers2

0

Try this

import os

directory = r'C:\Users\sample_dir'
for filename in os.listdir(directory):
    -- some operation --
b0neng4
  • 76
  • 7
  • I have added the code . I need var path will have a path of each file . Once it completes its request of uploading one file, it will open the next file. – HIMANSHU CHORSIYA Jun 04 '21 at 14:01
  • I have added the code . I need var path will have a path of each file . Once it completes its request of uploading one file, it will again go into loop and put path of the second file in" var path "open the next file. – HIMANSHU CHORSIYA Jun 04 '21 at 14:06
0

You can read paths line by line with the for loop you're using.

You're reading in read-binary (rb) mode, so you'll have to decode it later. Or simply use r.

For a proper example look here.

After you've created a list of paths, Iterate through each path.

for file in paths_list:
   upload_function(file)