python给txt文件批量二次原创源码

python给txt文件批量二次原创源码

python给txt文件批量二次原创源码-源码网
python给txt文件批量二次原创源码
此内容为付费阅读,请付费后查看
368
立即购买
您当前未登录!建议登陆后购买,可保存购买订单
付费阅读
import os,re
import shutil
import time
import requests

def walk_folder(root_path):
arr = []
for (root, dirs, files) in os.walk(root_path):
for filename in files:
filepath = os.path.join(root, filename)
arr.append(filepath)
return arr

# srcfile 需要复制、移动的文件
# dstpath 目的地址

def mymovefile(srcfile, dstpath): # 移动函数
if not os.path.isfile(srcfile):
print(“%s not exist!” % (srcfile))
else:
fpath, fname = os.path.split(srcfile) # 分离文件名和路径
if not os.path.exists(dstpath):
os.makedirs(dstpath) # 创建路径
dpath = dstpath + ‘/’ + fname
shutil.move(srcfile, dpath) # 移动文件 shutil.move(srcfile, dpath) #复制shutil.copy
print(“move %s -> %s” % (srcfile, dstpath + fname))

def open_f(p):
try:
c = open(p,‘r’,encoding=‘utf8’)
t = c.read()
except:
try:
c = open(p, ‘r’, encoding=‘gbk’)
t = c.read()
except:
print(‘error file’,p)
return
c.close()
return t

#=====二次原创接口====#
#file1 扫描文件

def aiwrite_txt(source_dir):
se = requests.session()
Post_url = “” #写作助手key
with open(source_dir, ‘r’, encoding=‘utf-8’) as f:
neirong = f.read()
print(原文:,neirong)
Post_data = {
“wenzhang”: neirong
}
Text = se.post(Post_url, data=Post_data).text.replace(水槽, 鱼缸).replace(‘/’, ‘/’)#替换标点或者不合适的词
output_file = open(source_dir, ‘w’, encoding=‘utf-8’)
output_file.write(Text)
print(二次原创中…”)
f.close()
output_file.close()
return Text

#=====二次原创接口====#


if __name__ == ‘__main__’:

root_path = r’D:\pycharm\www.busuanzi.net\txt’ # 扫哪个目录下的文件
dstpath = r’C:\Users\22\Desktop\0′ # 二次原创后文件目录
i = 0;
print(程序开始)
print(“================================”)
time_start = time.time()
for file in walk_folder(root_path):
print(当前处理的文件是第+ str(i + 1) + )
if file.endswith(‘.txt’):
print(file)
aiwrite_txt(file)
mymovefile(file, dstpath) #原创移动文件到dstpath目录,不需要可用#注释掉
i = i + 1;
time.sleep(2)
time_end = time.time()
time_sum = time_end – time_start
print(程序结束,操作用时【秒】+ str(time_sum))

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容