迭代目录通过API发布图片

时间:2017-10-12 22:49:13

标签: python foreach iteration filepath

我想对目录中的每个文件运行此Web调用,将路径"C:\\Users\\user\\Pictures\\279259.jpg\"变为Web调用。我想我可以使用os.path.join,但不知道如何格式化任何帮助将被赞赏

import http.client

conn = http.client.HTTPSConnection("some.website.com")

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"; filename=\"C:\\Users\\user\\Pictures\\279259.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"

headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",

    'cache-control': "no-cache",

    }

conn.request("POST", "/api/users/pictures", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

1 个答案:

答案 0 :(得分:0)

如果有人有一个更优雅的解决方案,那么我就会解决这个问题。

import os
import http.client

Files={}

path = "path"

for folder, subfolder, files in os.walk(os.getcwd()):
    for file in files:
        Files[file] = os.path.dirname(file)
for file in Files:
    print ((path) + (file))
    conn = http.client.HTTPSConnection("website")

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"; filename=\""+((path)+(file))+"\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"