用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
0
投稿
猜你喜欢
- pop()方法从列表移除并返回最后一个对象或obj。语法以下是pop()方法的语法:list.pop(obj=list[-1])
- 下面是虚机维护中,经常碰到的一些ASP程序中的数据库调用的错误,现收集整理如下:
- 目前定时的操作有:一、Html页面的定时刷新(Refresh--刷新 ) 1,Refresh (刷新) 代码使用说明 说明:让网页多长时间(
- wheel文件Wheel和Egg都是python的打包格式,目的是支持不需要编译或制作的安装过程,实际上也是一种压缩文件,将.whl的后缀改
- ASP木马防御: 代码如下:const adTypeBinary=1 dim jpg(1):jpg(0)=CB
- 前言Scrapy是一个非常好的抓取框架,它不仅提供了一些开箱可用的基础组建,还能够根据自己的需求,进行强大的自定义。本文主要给大家介绍了关于
- 前言 BeautifulSoup是主要以解析web网页的Python模块,它会提供一些强大的解释器,以解
- 前言分析目标网站的登录方式目标地址: https://github.com/login 登录方式做出分析:
- 本文实例讲述了用python实现面向对像的ASP程序的方法。分享给大家供大家参考。具体实现方法如下:平时我们写ASP时,一般都用vbscri
- 字典获取最大和最小value对应的keymy_dict = {'x':500, 'y':5874, '
- 导语:排版是一门艺术,也是一门技巧。我们每天都能在报纸,书籍等各种媒介上看到排版,或精美,或丑陋。如何能在准确传递信息的同时,又能排出精美的
- 这篇文章主要介绍了如何给Python代码进行加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 1.API接口:hello world 案例from flask import Flaskfrom flask_restful import
- 如今WEB的安全问题影响着整个安全界,SQL注入,跨站脚本攻击等攻击受到了关注。 网络安全问题日益变的更加重要,国内依然有很多主机受到此类安
- 关于django celery的使用网上有很多文章,本文就不多做更多的说明。本文使用版本python==3.8.15Django==3.2.
- Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台。连接数据库 注意:假设现有几台在同一局域网内的
- 就前面所讲,函数的基本内容已经完毕。但是,函数还有很多值得不断玩味的细节。这里进行阐述。参数的传递python中函数的参数通过赋值的方式来传
- 在asp中调用sql server的存储过程可以加快程序运行速度,本文介绍了asp使用存储过程的方法。1.调用存储过程的一般方法 先假设在s
- 本文实例讲述了Python科学计算包numpy用法。分享给大家供大家参考,具体如下:1 数据结构numpy使用一种称为ndarray的类似M
- 本文实例讲述了Python中random模块用法。分享给大家供大家参考。具体如下:import randomx = random.randi