用python批量移动文件
作者:风中狂笑 发布时间:2022-12-21 10:48:43
标签:python,批量,移动,文件
我是用来移动图片的,其他格式的文档也是可以的,改下后缀列表就可以了
import os,shutil
import datetime
#将文件夹里的图片全部移动到新文件夹中
#revised by Stephen Shen 2020-3-10 09:28:50
def 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+fext
dstpath=os.path.join(fdirname,newfbasename)
return dstpath
def 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:
continue
print('done')
然后再删除空文件夹
import os,shutil
#将文件夹里的空文件夹删除
#revised by Stephen Shen 2020-3-8 17:50:24
inpath=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')
来源:https://www.cnblogs.com/dogingate/p/12524319.html


猜你喜欢
- 本文为大家分享了virtualenv建立多个Python独立虚拟开发环境,供大家参考,具体内容如下1、安装virtualenv:pip in
- MySQL是一个关系型数据库管理系统,官方网站是http://www.mysql.com/,分为MySQL Enterprise Editi
- 分享一个sql数据库面试题。问题:表 table1,主键为 ID,ID为自动编号(ID可能不连续),要求查询第31-40行记录,请问SQL语
- 引用是什么在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量
- 简介特点请求你所要的数据,不多不少获取多个资源,只用一个请求描述所有的可能,类型系统几乎所有语言支持文档Graphene-PythonGra
- Declare @Id varchar(20) Declare @Name varchar(20) Declare Cur Cursor F
- 一、在vs中新建一个工程,然后新建一个源文件二、右击上述图片中的mysql_test工程名,然后在最下方找到属性,并点击三、点击VC++目录
- 本文介绍了python BlockingScheduler定时任务及其他方式的实现,具体如下:#BlockingScheduler定时任务f
- 1.CNN卷积层通过nn.Conv2d可以设置卷积层,当然也有1d和3d。卷积层设置完毕,将设置好的输入数据,传给layer(),即可完成一
- python-pymysql获取字段名称-获取内容获取字段名称-获取内容import pymysql# 连接数据库db = pymysql.
- 教大家做一个简易的猜数字的游戏,但确实蛮简单的,我这个四年级的小盆友也才学了一天,所以给那些不会的教一下。我们先来看导入模块:"&
- 使用PyCharm工具写好的Python程序脚本,怎么将.py文件编译为可执行的.exe文件前提是已经安装了Python环境。第一步:在Py
- 需要selenium控制的chrome向下滑动,自动加载一些内容,核心代码是:browser.execute_script("wi
- 网页的布局也许是大家最不放在眼里的地方,其实布局地位如同文字的排版一样,随便可布,布即随便。但是看过我上篇《网页设计技巧系列 之 文本排版》
- python 中sorted与sort有什么区别sort(cmp=None, key=None, reverse=False)sorted(
- 在mysql安装目录的bin下, 运行mysql --install MYSQL5查看服务中, 会多一个MYSQL5的服务XAMPP的控制面
- 举例如下:<HTML> <HEAD>  
- 建表、insert数据create table tmp_login ( user_id int(11) , logi
- 本文实例讲述了centos6使用docker部署redis主从数据库操作。分享给大家供大家参考,具体如下:目录结构:/redis
- 一、事件捕捉(Event Capture)的实现问题首先在说这件事前,先感谢一下Realazy。 W3C DOM Level2的事