时间:2021-05-22
我是用来移动图片的,其他格式的文档也是可以的,改下后缀列表就可以了
import os,shutilimport datetime#将文件夹里的图片全部移动到新文件夹中#revised by Stephen Shen 2020-3-10 09:28:50def renameFile(dstpath):fdirname,fbasename=os.path.split(dstpath)#文件名相同但大小不同fname,fext=os.path.splitext(fbasename)nowtime=datetime.datetime.now()strtime=str(nowtime.year)+str(nowtime.month)+str(nowtime.day)+str(nowtime.hour)+str(nowtime.minute)newfbasename=fname+'-'+strtime+fextdstpath=os.path.join(fdirname,newfbasename)return dstpathdef moveFile(oldpath,newpath):if os.path.exists(newpath):newpath=renameFile(newpath)try:shutil.move(oldpath,newpath)print(oldpath+' is moved')except:print(oldpath+' is skipped')inpath=r'K:\fileExtracted\imagesFromDocs'outpath=r'K:\filesExtracted'image_ext=['.JPG','.jpg','.png','.PNG','.jpeg','.wdp']image_outpath=os.path.join(outpath,'image')doc_ext=['.doc','.docx']doc_outpath=os.path.join(outpath,'doc')emf_ext=['.emf']emf_outpath=os.path.join(image_outpath,'emf')wmf_ext=['.wmf']wmf_outpath=os.path.join(image_outpath,'wmf')if not os.path.exists(outpath):os.makedirs(outpath)if not os.path.exists(image_outpath):os.makedirs(image_outpath)if not os.path.exists(doc_outpath):os.makedirs(doc_outpath)if not os.path.exists(emf_outpath):os.makedirs(emf_outpath)if not os.path.exists(wmf_outpath):os.makedirs(wmf_outpath)for folder,subfolders,files in os.walk(inpath):for file in files:oldpath=os.path.join(folder,file)if os.path.splitext(file)[-1] in image_ext:newpath=os.path.join(image_outpath,file)moveFile(oldpath,newpath)elif os.path.splitext(file)[-1] in doc_ext:newpath=os.path.join(doc_outpath,file)moveFile(oldpath,newpath)elif os.path.splitext(file)[-1] in emf_ext:newpath=os.path.join(emf_outpath,file)moveFile(oldpath,newpath)elif os.path.splitext(file)[-1] in wmf_ext:newpath=os.path.join(wmf_outpath,file)moveFile(oldpath,newpath)else:continueprint('done')然后再删除空文件夹
import os,shutil#将文件夹里的空文件夹删除#revised by Stephen Shen 2020-3-8 17:50:24inpath=r'E:\pics-moving\待分类照片'for folder,subfolders,files in os.walk(inpath):if not os.listdir(folder):shutil.rmtree(folder)# print(folder+' is empyt')print(folder+' is deleted')print('done')以上就是用python批量移动文件的详细内容,更多关于python批量移动文件的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文通过实例为大家分享了python实现批量移动文件的具体代码,供大家参考,具体内容如下任务:每个大文件夹下有许多小文件夹,将小文件夹里的pdf文件移动到指定文
今天写一个在windows下批量修改文件名的python脚本,用到文件的遍历。用python进行文件遍历有多种方法,这里列举并说明一下。os.path.walk
今天遇到一个任务,给一个excel文件,里面有500多个pdf文件的下载链接,需要把这些文件全部下载下来。我知道用python爬虫可以批量下载,不过之前没有接触
本文实例讲述了Python实现批量修改文件名的方法。分享给大家供大家参考。具体如下:下载了评书《贺龙传奇》,文件名中却都含有xxx有声下载,用脚本将其去掉。脚本
Python实现批量下载文件#!/usr/bin/envpython#-*-coding:utf-8-*-fromgeventimportmonkeymonke