用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进程通信之匿名管道实例讲解
Apr 11 Python
详解Django中的form库的使用
Jul 18 Python
Python实现模拟登录及表单提交的方法
Jul 25 Python
python模拟Django框架实例
May 17 Python
不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决
Sep 24 Python
Python 等分切分数据及规则命名的实例代码
Aug 16 Python
python+gdal+遥感图像拼接(mosaic)的实例
Mar 10 Python
django列表筛选功能的实现代码
Mar 27 Python
python 使用cycle构造无限循环迭代器
Dec 02 Python
如何通过python检查文件是否被占用
Dec 18 Python
MoviePy常用剪辑类及Python视频剪辑自动化
Dec 18 Python
使用Python下载抖音各大V视频的思路详解
Feb 06 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
PHP 日常开发小技巧
2009/09/23 PHP
eaglephp使用微信api接口开发微信框架
2014/01/09 PHP
php中使用Ajax时出现Error(c00ce56e)的详细解决方案
2014/11/03 PHP
linux中cd命令使用详解
2015/01/08 PHP
php实现用于验证所有类型的信用卡类
2015/03/24 PHP
PHP中is_file()函数使用指南
2015/05/08 PHP
PHP图片水印类的封装
2017/07/06 PHP
学习ExtJS table布局
2009/10/08 Javascript
Javascript实现CheckBox的全选与取消全选的代码
2010/07/20 Javascript
javascript获取作用在元素上面的样式属性代码
2012/09/20 Javascript
js向上无缝滚动,网站公告效果 具体代码
2013/11/18 Javascript
Javascript setInterval的两种调用方法(实例讲解)
2013/11/29 Javascript
通过jquery 获取URL参数并进行转码
2014/08/18 Javascript
js实现页面跳转的五种方法推荐
2016/03/10 Javascript
详解nodejs与javascript中的aes加密
2016/05/22 NodeJs
微信小程序 地图(map)实例详解
2016/11/16 Javascript
从零开始做一个pagination分页组件
2017/03/15 Javascript
JS基于递归实现网页版计算器的方法分析
2017/12/20 Javascript
vue-cli配置全局sass、less变量的方法
2019/06/06 Javascript
mock.js模拟前后台交互
2019/07/25 Javascript
微信小程序弹窗禁止页面滚动的实现代码
2020/12/30 Javascript
[06:16]第十四期-国士无双绝地翻盘之撼地神牛
2014/06/24 DOTA
Python中tell()方法的使用详解
2015/05/24 Python
python 字符串和整数的转换方法
2018/06/25 Python
python线程join方法原理解析
2020/02/11 Python
python GUI库图形界面开发之PyQt5日期时间控件QDateTimeEdit详细使用方法与实例
2020/02/27 Python
使用Python获取当前工作目录和执行命令的位置
2020/03/09 Python
Pycharm无法打开双击没反应的问题及解决方案
2020/08/17 Python
浅析NumPy 切片和索引
2020/09/02 Python
世界领先的在线地板和建筑材料批发商:BuildDirect
2017/02/26 全球购物
Banana Republic欧盟:美国都市简约风格的代表品牌
2018/05/09 全球购物
巴西儿童时尚购物网站:Dinda
2019/08/14 全球购物
环保项目建议书
2014/08/26 职场文书
2014年教师节演讲稿范文
2014/09/10 职场文书
保研专家推荐信范文
2015/03/25 职场文书
Mysql调整优化之四种分区方式以及组合分区
2022/04/13 MySQL