记录一下,方便以后翻阅
import os
import shutil
#待导出文件夹路径
source_path = os.path.abspath('D:/XXX')
#目标文件夹路径,路径最后的斜杠不能丢!!!
target_path = 'D:/XXX/'
# 导出文件编号初始值
i=1
if not os.path.exists(target_path):
os.makedirs(target_path)
if os.path.exists(source_path):
# root 所指的是当前正在遍历的这个文件夹的本身的地址
# dirs 是一个 list,内容是该文件夹中所有的目录的名字(不包括子目录)
# files 同样是 list, 内容是该文件夹中所有的文件(不包括子目录)
for root, dirs, files in os.walk(source_path):
for file in files:
# 指定特定类型的文件,示例 if (os.path.splitext(file)[0] [0] == 'G' and os.path.splitext(file)[1] == '.npy' ):
if os.path.splitext(file)[1] == '.tif':
src_file = os.path.join(root, file)
#导出文件命名方式
newtargetpath=target_path+str(i)+'XXX.tif'
shutil.copyfile(src_file, newtargetpath)
#输出被导出的文件【print src_file.decode('gbk') .decode('gbk')是解决中文显示乱码问题】
print(src_file)
i+=1
print('copy files finished!')
参考来源
https://www.cnblogs.com/dazhan/p/9834979.html
https://www.cnblogs.com/wodewei/p/11836641.html