用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 类与元类的深度挖掘 I【经验】
May 06 Python
Python的dict字典结构操作方法学习笔记
May 07 Python
python3实现读取chrome浏览器cookie
Jun 19 Python
selenium+python实现自动登录脚本
Apr 22 Python
PyCharm鼠标右键不显示Run unittest的解决方法
Nov 30 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
int在python中的含义以及用法
Jun 27 Python
Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法
Aug 26 Python
python科学计算之scipy——optimize用法
Nov 25 Python
使用Python实现牛顿法求极值
Feb 10 Python
Python利用 utf-8-sig 编码格式解决写入 csv 文件乱码问题
Feb 21 Python
python中 .npy文件的读写操作实例
Apr 14 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
smarty 原来也不过如此~~呵呵
2006/11/25 PHP
YII视图整合kindeditor扩展的方法
2016/07/13 PHP
PHP 微信扫码支付源代码(推荐)
2016/11/03 PHP
Yii2学习笔记之汉化yii设置表单的描述(属性标签attributeLabels)
2017/02/07 PHP
win10 apache配置虚拟主机后localhost无法使用的解决方法
2018/01/27 PHP
jquery dialog键盘事件代码
2010/08/01 Javascript
javascript中call和apply方法浅谈
2013/09/27 Javascript
js中document.write使用过程中的一点疑问解答
2014/03/20 Javascript
jquery浏览器滚动加载技术实现方案
2014/06/03 Javascript
JavaScript中使用typeof运算符需要注意的几个坑
2014/11/08 Javascript
js兼容pc端浏览器并有多种弹出小提示的手机端浮层控件实例
2015/04/29 Javascript
个人总结的一些JavaScript技巧、实用函数、简洁方法、编程细节
2015/06/10 Javascript
详谈$.data()的用法和作用
2017/02/13 Javascript
vue.js实现含搜索的多种复选框(附源码)
2017/03/23 Javascript
Node.js搭建小程序后台服务
2018/01/03 Javascript
Vue.js进阶知识点总结
2018/04/01 Javascript
vue点击input弹出带搜索键盘并监听该元素的方法
2018/08/25 Javascript
Angular5中状态管理的实现
2018/09/03 Javascript
vue项目中,main.js,App.vue,index.html的调用方法
2018/09/20 Javascript
详解vue-router 动态路由下子页面多页共活的解决方案
2019/12/22 Javascript
微信小程序实现下拉加载更多商品
2020/12/29 Javascript
python操作日期和时间的方法
2014/03/11 Python
Python调用C语言的方法【基于ctypes模块】
2018/01/22 Python
Django项目实战之用户头像上传与访问的示例
2018/04/21 Python
Python常用模块logging——日志输出功能(示例代码)
2019/11/20 Python
基于TensorFlow常量、序列以及随机值生成实例
2020/01/04 Python
pytorch实现Tensor变量之间的转换
2020/02/17 Python
python如何把字符串类型list转换成list
2020/02/18 Python
使用Python文件读写,自定义分隔符(custom delimiter)
2020/07/05 Python
Beauty Expert美国/加拿大:购买奢侈美容产品
2018/12/05 全球购物
Fabletics官网:美国运动服饰品牌,由好莱坞女演员凯特·哈德森创立
2019/10/19 全球购物
个人充满哲理的自我评价
2014/02/20 职场文书
物业保安岗位职责
2014/07/02 职场文书
2015年度高中教师工作总结
2015/05/26 职场文书
Python Pandas知识点之缺失值处理详解
2021/05/11 Python
新手必备Python开发环境搭建教程
2021/05/28 Python