import pandas as pdnames = pd.read_csv('sub1009.csv',header=None)# names.head()import os,shutildef movefile(srcfile,dstfile): if not os.path.isfile(srcfile): print('%s not exist!'%(srcfile)) else: fpath,fname=os.path.split(dstfile) #分离文件名和路径 if not os.path.exists(fpath): os.makedirs(fpath) #创建路径 shutil.move(srcfile,dstfile) #移动文件 print('move %s -> %s'%( srcfile,dstfile))tmp = list(names.values)for file in tmp: movefile('./testb/'+str(file[0]),'./'+ str(file[1]) + '/' +str(file[0]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
将子文件夹所有文件 复制到 某一文件夹
import os, shutil # 绝对路径 src_dir = os.path.abspath(r'C:\Users\Yaotc\Desktop\hi\train_2018') dst_dir = os.path.abspath(r'C:\Users\Yaotc\Desktop\hi\data') if not os.path.exists(dst_dir): os.makedirs(dst_dir) if os.path.exists(src_dir): # root 所指的是当前正在遍历的这个文件夹的本身的地址 # dirs 是一个 list,内容是该文件夹中所有的目录的名字(不包括子目录) # files 同样是 list, 内容是该文件夹中所有的文件(不包括子目录) for root,dirs,files in os.walk(src_dir): for file in files: src_file = os.path.join(root, file) shutil.copy(src_file, dst_dir) print(src_file) print('Done!')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21