import os
import sys
import io
import time
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #改变标准输出的默认编码
def read():
path = r"C:\Users\mzj\Desktop\高精准"# 文件夹目录
for i in os.listdir(path):
try:
for q in os.listdir(os.path.join(path,i)):
paths=os.path.join(path,i,q)
f = open(paths, "r", encoding="UTF-8")
lines = f.readlines()
f.close()
url = os.path.basename(paths)
li = url.split('_')
temp_lines_list = [x.strip() for x in lines]
# print(temp_lines_list)
if len(temp_lines_list) < 2:
# 行数不够,不进行处理
print('行数太短了。。。。。。')
# #去掉空行
for t in temp_lines_list:
download_file_path = r'C:\Users\mzj\Desktop'
now_title = t.split("|")
while now_title:
if now_title[8] == 'ztb':
download_path = os.path.join(download_file_path, now_title[8])
if not os.path.exists(download_path):
os.mkdir(download_path)
time_path = os.path.join(download_path, str(li[0][:4] + '-' + li[0][4:6] + '-' + li[0][6:8]))
if not os.path.exists(time_path):
os.mkdir(time_path)
urls = time_path + '/' + str(
li[0] + '_' + li[1] + '_' + str(now_title[8]) + '_' + li[3] + '_' + li[4])
with open(urls, 'a', encoding='utf-8')as f:
f.write(t + '\n')
break
else:
break
except Exception as e:
# 文件读取失败
print(e)
#去掉\n
if __name__=='__main__':
read()
import os
path=r'C:\Users\mzj\Desktop\city\city\out'
for i in os.listdir(path):
with open(os.path.join(path,i),'r',encoding='utf-8')as r:
for w in r.readlines():
with open(r'C:\Users\mzj\Desktop\city\city\two.out','a+',encoding='utf-8')as f:
f.write(w)
os.remove(os.path.join(path,i))
print('写入成功{},删除成功{}'.format(os.path.join(path,i),os.path.join(path,i)))