运行结果:
完整代码:
import os
import shutil
import time
path = input('请输入源路径')
topath = input('请输入新路径')
NUM_COUNT = 0
def seltxt(path,topath):
global NUM_COUNT
for root, dirs, files in os.walk(path):
for file in files:
# L.append(os.path.join(file))
if os.path.splitext(file)[1] == '.txt':
file_path = os.path.join(root, file)
file_name = os.path.join(file)
NUM_COUNT = NUM_COUNT + 1
#print('成功', NUM_COUNT, file_path)
#print('成功', NUM_COUNT, file_name)
file_parpath = topath+'/'
if os.path.exists(file_parpath):
pass
# 复制到指定目录,首先创建一个目录
else:
os.mkdir(file_parpath)
fromfile = file_path
tofile = file_parpath+file_name
if os.path.exists(tofile):
now = str(int(time.time()))
tofile = file_parpath+now+file_name
# 复制到指定目录,首先创建一个目录
else:
pass
print('成功', NUM_COUNT, tofile)
shutil.copy(fromfile,tofile)
seltxt(path,topath)