import os
import shutil
def findSameNameVdnnandcopy(src):
#获取与文件夹同名的文件
for root,dirs,names in os.walk(src):#遍历所有子孙目录及文件,root:str类型,所有子孙文件夹绝对路径;dirs:list,子孙文件夹名称;names:list,子孙文件名
for name in names:#因为names为列表无法和str比较
# name=''.join(name) #列表转字符串str
dirname=root.split('\\')[2]#src下的子目录名
if name==dirname+'.vdnn':#需要找的vdnn文件名
sourcefile=root+'\\'+name#与文件夹同名vdnn的源地址
destinstionfile='E:\\model\\'
print(sourcefile,os.path.getsize(sourcefile),destinstionfile,dirname)#调试使用可删除
if not os.path.exists(destinstionfile):
os.mkdir(destinstionfile)
try:
shutil.copy(sourcefile,destinstionfile)#复制文件
except shutil.SameFileError :
print(name,'文件已存在')
def findInputBinAndCopyRename(src):
#获取与文件夹同名的文件并改名
for root,dirs,names in os.walk(src):#遍历所有子孙目录及文件,root:str,所有子孙文件夹路径;dirs:list,子孙文件夹名称;names:list,子孙文件名
for name in names:#因为names为列表无法和str比较
# name=''.join(name) #列表转字符串str
dirname=root.split('\\')[2]
if name=='input_0_float.bin' or name=='input_float.bin':#需要找的bin文件名
if not os.path.exists('E:\\input\\'):#如果不存在保存input的目录,就创建
os.mkdir('E:\\input\\')
sourcefile=root+'\\'+name#bin的源地址
destinstionfile='E:\\input\\'+dirname+'_0'+'.bin'
print(sourcefile,os.path.getsize(sourcefile),destinstionfile,dirname)#调试使用可删除
try:
shutil.copy(sourcefile,destinstionfile)#复制文件,此功能可以在复制时改名
except shutil.SameFileError :
print(name,'文件已存在')
break
print('请输入地址:')
src=str(input())
findSameNameVdnnandcopy(src)
findInputBinAndCopyRename(src)
python 复制某文件夹下与文件同名的文件,并复制(提取)到其他文件夹中
于 2022-07-26 15:21:30 首次发布