分享

Python 批量移动文件

 乙甲壬 2020-07-06
  • 将./testb下的所有jpg文件,按照Label移动到不同文件夹

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多