使用python拷贝或者剪切指定后缀文件
2018-08-20 本文已影响0人
远航天下
代码如下:
def zip_file(old_path,new_path):
if get_file_count(old_path) >= 10:
for root, dirs, files in os.walk(old_path):
for i in range(len(files)):
# print(files[i])
if (files[i][-4:] == '.log')or (files[i][-5:] == '.html'):
file_path = old_path + files[i]
# print(file_path)
new_file_path = new_path #+ files[i]
# print(new_file_path)
try:
shutil.move(file_path, new_file_path)
logger.info("{a}条剪切成功!".format(a=i))
except Exception as e:
logger.info("剪切失败:{a}".format(a=e))
else:
print("文件少于10条,不需要剪切!")
PS:shutil.move() # 剪切文件;shutil.copy() # 拷贝文件