用python批量移动文件
作者:风中狂笑 时间:2022-12-21 10:48:43
我是用来移动图片的,其他格式的文档也是可以的,改下后缀列表就可以了
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
标签:python,批量,移动,文件
0
投稿
猜你喜欢
解决Vue axios post请求,后台获取不到数据的问题方法
2024-05-09 09:38:38
Cython编译python为so 代码加密示例
2023-01-22 13:16:48
Webform 内置对象 Session对象、Application全局对象,ViewState详细介绍
2023-06-30 02:14:07
perl ping检测功能脚本代码
2023-09-27 12:41:47
全方位清理浮动
2009-06-16 14:51:00
Python 将代码转换为可执行文件脱离python环境运行(步骤详解)
2023-05-02 13:29:31
最新anaconda安装配置教程
2021-07-17 05:05:34
python绘制横向水平柱状条形图
2022-01-10 01:01:49
基于Python实现Excel转Markdown表格
2021-04-27 17:05:04
Python实现邮件发送功能的方法详解
2021-02-02 10:34:58
在Django中创建动态视图的教程
2021-09-25 09:17:28
js实现无刷新监听URL的变化示例代码详解
2024-04-17 09:43:16
javascript农历日历及世界时间代码
2007-12-21 13:25:00
pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异
2021-08-31 05:15:21
Internet Explorer 8 Beta2 功能预览
2008-07-29 13:20:00
MySQL的id关联和索引使用的实际优化案例
2024-01-26 03:43:30
CSS 超链接图标规范 V1.0
2007-12-28 12:05:00
Django ValuesQuerySet转json方式
2021-12-05 07:15:34
Python 模拟员工信息数据库操作的实例
2024-01-20 03:42:04
用户 jb51net 登录失败。原因: 该帐户的密码必须更改
2024-01-13 05:58:46