1.遍历文件夹及子文件夹,并且复制制定文件
import os
import shutil
def get_filelist(dir):
if os.path.isfile(dir):
pass
elif os.path.isdir(dir):
for s in os.listdir(dir):
newDir = os.path.join(dir, s)
if s == "084_X02_P01":
cp_file(newDir)
get_filelist(newDir)
return
def cp_file(dir):
savepath = '/dell_nas/common/group/zym/data84/'
for s in os.listdir(dir):
if s.endswith('.jpg'):
txt = s.replace('.jpg', '.txt')
f = open(dir +'/' + txt, 'r')
tmp = f.readlines()
tfname = tmp[3][:-1]
tfname = str(tfname)
if not os.path.exists(savepath + tfname):
os.mkdir(savepath + tfname)
shutil.copyfile(dir + '/' + s, savepath + tfname + '/' + s)
f.close()
if __name__ =='__main__' :
list = get_filelist('/dell_nas/common/TFDS/ND_text')
2.