import os
basepath = ‘./‘
def get_file_from_path(path):
if os.path.isdir(path):
# 处理这个文件夹
for item in os.listdir(path):
next_path=os.path.join(path, item)
get_file_from_path(next_path)
print("All item in dir_path", next_path, " has been processed")
else:
# 处理这个文件
from shutil import copyfile
copyfile(frompath,topath)
fp = open(filepath, ‘a‘)
fp.write(stringtowrite)
fp.close()
fp = open(filepath, ‘r‘)
lines=fp.readlines()
for line in lines:
#处理每一行
fp.close()
if not os.path.exists(dir_path):
os.mkdir(dir_path) #存在会报错
原文:https://www.cnblogs.com/Ryan16231112/p/13144445.html