Python-for循环选择目录中的每个文件



我在python 3中有一个简单的查询。我有一个名为var的路径,我将从目录中传递文件路径。File out.txt包含许多文件的列表路径

示例:out.txt

c:/python/text1.txtc:/python/text2.txtc:/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)

如果将这样运行>上传c:/python/text1.txt…然后在操作后返回上传下一个文件

试试这个

import os
directory = r'C:Userssample_dir'
for filename in os.listdir(directory):
-- some operation --

您可以使用正在使用的for循环逐行读取路径。

您正在以read-binary(rb(模式读取,因此稍后必须对其进行解码。或者简单地使用r

关于一个合适的例子,请看这里。

创建路径列表后,遍历每个路径。

for file in paths_list:
upload_function(file)

最新更新