代码:
# coding=utf-8 import shutil import traceback import os import base64 import json import requests from pprint import pprint def move_file(src_path, dst_path, file): print 'from : ', src_path print 'to : ', dst_path try: # cmd = 'chmod -R +x ' + src_path # os.popen(cmd) f_src = os.path.join(src_path, file) if not os.path.exists(dst_path): os.mkdir(dst_path) f_dst = os.path.join(dst_path, file) shutil.move(f_src, f_dst) except Exception as e: print 'move_file ERROR: ', e traceback.print_exc() # Init url = "https://www.xacisco.net/blog/wp-json/wp/v2/posts" username = "xxx" password = "xxx" credentials = "{}:{}".format(username, password) token = base64.b64encode(credentials.encode()) # Header headers = { "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36", "Authorization": "Basic {}".format(token.decode("utf-8")), "content-type": "application/json", } path = "D:\桌面文件\pythoncode\xacisco-blog\文章" # 文件夹目录 files = os.listdir(path) # 得到文件夹下的所有文件名称 txts = [] for file in files: # 遍历文件夹 position = path + '\' + file # 构造路径,"\",其中一个''为转义符 # print (position) print('文章标题:' + os.path.splitext(file)[0]) # 文件标题 with open(position, "r", encoding='utf-8') as f: # 打开文件 data = f.read() # 读取文件 print('文章内容' + data) #这里是文件的内容 # Post info post = { "title": (os.path.splitext(file)[0]), "content": data, "status": "publish" } r = requests.post( url, headers=headers, json=post, ) print('开始发送....') print('发送结果:'+ r.text) #吧这个文件移动过去 src_path = 'D:\桌面文件\pythoncode\xacisco-blog\文章' dst_path = 'D:\桌面文件\pythoncode\xacisco-blog\已经读取' move_file(src_path,dst_path,file) print('移动成功了:' + file)