【发布时间】:2014-05-28 17:10:20
【问题描述】:
我可以通过执行以下操作来发布 一个 json 文件:
url = 'https://myWebsite.com/ext/ext/ext'
json_file = open("/Users/ME/folder/folder/folder/folder/test.json")
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json_file, headers=headers)
但是当我尝试使用 iglob 遍历目录中的所有 json 文件时:
url = 'https://myWebsite.com/ext/ext/ext'
json_files = glob.iglob("/Users/ME/Documents/folder/folder/folder/*.json")
for data in json_files:
test = {'file': open(data)}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=test, headers=headers)
服务器向我抛出了一些疯狂的错误,表明我发布了无效的 JSON 原语。我对这两种方法都使用了 exact 相同的 json 文件,但由于某种原因,第二种方法失败了。
【问题讨论】:
-
您是否尝试过 打印
json_files并查看您是否真的打开了相同的文件? -
print json_files = "
" and print json_file = " " -
我不太确定这意味着什么,但我知道它们是同一个文件,只是访问方式不同
-
iglob()返回一个生成器,在循环对象时按需生成文件名。print list(json_files)为您提供所有匹配项的列表。不过没关系,我发现了问题。
标签: python json python-requests glob