用python批量移动文件

 更新时间:2021年01月14日 11:11:54   作者:风中狂笑  
这篇文章主要介绍了如何用python批量移动文件,帮助大家更好的理解和使用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')

以上就是用python批量移动文件的详细内容,更多关于python批量移动文件的资料请关注脚本之家其它相关文章!

相关文章

  • 使用python半分钟轻松完成证件照换底色

    使用python半分钟轻松完成证件照换底色

    是不是很多小伙伴儿都不清楚公司是需要蓝底还是红底的证件照,今天小编直接带大家做一款Python换底色的一款小程序,不管什么底色儿,放马过来
    2021-09-09
  • python中常用的各种数据库操作模块和连接实例

    python中常用的各种数据库操作模块和连接实例

    这篇文章主要介绍了python中常用的各种数据库操作模块和连接实例,包括sqlite3、oracle、mysql、excel,需要的朋友可以参考下
    2014-05-05
  • Python面试之os.system()和os.popen()的区别详析

    Python面试之os.system()和os.popen()的区别详析

    Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容,下面这篇文章主要给大家介绍了关于Python面试之os.system()和os.popen()区别的相关资料,需要的朋友可以参考下
    2022-06-06
  • openstack中的rpc远程调用的方法

    openstack中的rpc远程调用的方法

    今天通过本文给大家分享openstack中的rpc远程调用的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-07-07
  • Python批量查询关键词微信指数实例方法

    Python批量查询关键词微信指数实例方法

    在本篇文章中小编给大家整理的是关于Python批量查询关键词微信指数实例方法以及相关代码,需要的朋友们可以跟着学习下。
    2019-06-06
  • python基础教程之基本内置数据类型介绍

    python基础教程之基本内置数据类型介绍

    在Python程序中,每个数据都是对像,每个对像都有自己的一个类型。不同类型有不同的操作方法,使用内置数据类型独有的操作方法,可以更快的完成很多工作
    2014-02-02
  • Tensor 和 NumPy 相互转换的实现

    Tensor 和 NumPy 相互转换的实现

    本文主要介绍了Tensor 和 NumPy 相互转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 关于Python turtle库使用时坐标的确定方法

    关于Python turtle库使用时坐标的确定方法

    这篇文章主要介绍了关于Python turtle库使用时坐标的确定方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Python datetime 如何处理时区信息

    Python datetime 如何处理时区信息

    这篇文章主要介绍了Python datetime 如何处理时区信息,帮助大家更好的用python 处理时间,感兴趣的朋友可以了解下。
    2020-09-09
  • PyQt5 文本输入框自动补全QLineEdit的实现示例

    PyQt5 文本输入框自动补全QLineEdit的实现示例

    这篇文章主要介绍了PyQt5 文本输入框自动补全QLineEdit的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05

最新评论