遍历一个文件夹下所有文件(文件夹可以嵌套)
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) #存在会报错