用python批量移动文件


Posted in Python onJanuary 14, 2021

我是用来移动图片的,其他格式的文档也是可以的,改下后缀列表就可以了

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')

以上就是用python批量移动文件的详细内容,更多关于python批量移动文件的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python局域网ip扫描示例分享
Apr 03 Python
Python网页解析利器BeautifulSoup安装使用介绍
Mar 17 Python
Python 查看文件的编码格式方法
Dec 21 Python
pandas数据清洗,排序,索引设置,数据选取方法
May 18 Python
OPENCV去除小连通区域,去除孔洞的实例讲解
Jun 21 Python
详解Django中六个常用的自定义装饰器
Jul 04 Python
python中 * 的用法详解
Jul 10 Python
Django 数据库同步操作技巧详解
Jul 19 Python
python基于socket进行端口转发实现后门隐藏的示例
Jul 25 Python
pytorch 在sequential中使用view来reshape的例子
Aug 20 Python
Python引入多个模块及包的概念过程解析
Sep 21 Python
python利用platform模块获取系统信息
Oct 09 Python
python用700行代码实现http客户端
Jan 14 #Python
python批量生成身份证号到Excel的两种方法实例
Jan 14 #Python
Django扫码抽奖平台的配置过程详解
Jan 14 #Python
如何用python实现一个HTTP连接池
Jan 14 #Python
如何用python写个模板引擎
Jan 14 #Python
opencv python 对指针仪表读数识别的两种方式
Jan 14 #Python
详解如何使用Pytest进行自动化测试
Jan 14 #Python
You might like
DIY一个适配电脑声卡的动圈话筒放大器
2021/03/02 无线电
php 删除一个数组中的某个值.兼容多维数组!
2012/02/18 PHP
PHP 文件编程综合案例-文件上传的实现
2013/07/03 PHP
PHP后台微信支付和支付宝支付开发
2017/04/28 PHP
PHP实现通过文本文件统计页面访问量功能示例
2019/02/13 PHP
php中使用array_filter()函数过滤数组实例讲解
2021/03/03 PHP
jQuery对象和Javascript对象之间转换的实例代码
2013/03/20 Javascript
删除select中所有option选项jquery代码
2013/08/12 Javascript
jQuery 1.9使用$.support替代$.browser的使用方法
2014/05/27 Javascript
JavaScript中的数组操作介绍
2014/12/30 Javascript
基于jQuery+PHP+Mysql实现在线拍照和在线浏览照片
2015/09/06 Javascript
getElementById().innerHTML与getElementById().value的区别
2016/10/27 Javascript
Angularjs自定义指令Directive详解
2017/05/27 Javascript
vue elementui form表单验证的实现
2018/11/11 Javascript
Vue源码探究之状态初始化
2018/11/14 Javascript
[48:22]VGJ.S vs VG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[02:23]完美世界全国高校联赛街访DOTA2第一期
2019/11/28 DOTA
Python导入txt数据到mysql的方法
2015/04/08 Python
python中__slots__用法实例
2015/06/04 Python
python实现发送和获取手机短信验证码
2016/01/15 Python
利用Python中的pandas库对cdn日志进行分析详解
2017/03/07 Python
CSS3中媒体查询结合rem布局适配手机屏幕
2019/06/10 HTML / CSS
Lancome兰蔻官方旗舰店:来自法国的世界知名美妆品牌
2018/06/14 全球购物
Java工程师面试集锦之Spring框架
2013/06/16 面试题
零件设计自荐信范文
2013/11/27 职场文书
电子商务专业推荐信范文
2013/12/02 职场文书
纪念建党演讲稿范文
2014/01/13 职场文书
《雾凇》教学反思
2014/02/17 职场文书
经营理念口号
2014/06/21 职场文书
校庆标语集锦
2014/06/25 职场文书
竞选班干部演讲稿500字
2014/08/20 职场文书
支行行长岗位职责
2015/02/15 职场文书
幼师辞职信怎么写
2015/02/27 职场文书
个人政治思想总结
2015/03/05 职场文书
浅谈JS的二进制家族
2021/05/09 Javascript
Vue elementUI表单嵌套表格并对每行进行校验详解
2022/02/18 Vue.js