用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实现字典(dict)和字符串(string)的相互转换方法

    python实现字典(dict)和字符串(string)的相互转换方法

    这篇文章主要介绍了python实现字典(dict)和字符串(string)的相互转换方法,涉及Python字典dict的遍历与字符串转换相关操作技巧,需要的朋友可以参考下
    2017-03-03
  • python3将视频流保存为本地视频文件

    python3将视频流保存为本地视频文件

    这篇文章主要为大家详细介绍了python3将视频流保存为本地视频文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • django实现同一个ip十分钟内只能注册一次的实例

    django实现同一个ip十分钟内只能注册一次的实例

    下面小编就为大家带来一篇django实现同一个ip十分钟内只能注册一次的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • 在Python的Flask框架中验证注册用户的Email的方法

    在Python的Flask框架中验证注册用户的Email的方法

    这篇文章主要介绍了在Python的Flask框架中验证注册用户的Email的方法,包括非常详细的测试过程,极力推荐!需要的朋友可以参考下
    2015-09-09
  • 实时获取Python的print输出流方法

    实时获取Python的print输出流方法

    今天小编就为大家分享一篇实时获取Python的print输出流方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 在Python中计算移动平均值的方法

    在Python中计算移动平均值的方法

    在这篇文章中,我们将看到如何在Python中计算移动平均值,移动平均是指总观测值集合中固定大小子集的一系列平均值,它也被称为滚动平均,文中通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-10-10
  • 详解 Python中LEGB和闭包及装饰器

    详解 Python中LEGB和闭包及装饰器

    这篇文章主要介绍了详解 Python中LEGB和闭包及装饰器的相关资料,主要介绍了函数作用域和闭包的理解和使用方法及Python中的装饰器,需要的朋友可以参考下
    2017-08-08
  • VScode查看python f.write()的文件乱码问题及解决方法

    VScode查看python f.write()的文件乱码问题及解决方法

    这篇文章主要介绍了VScode查看python f.write()的文件乱码问题及解决方法,本文通过图文并茂的形式给大家分享解决方法,需要的朋友可以参考下
    2023-02-02
  • Numpy的np.random随机模块详解

    Numpy的np.random随机模块详解

    这篇文章主要介绍了Numpy的np.random随机模块详解,平时都会使用到随机模块,一般是torch.random或者是numpy.random,有或者是直接使用ramdom这个python内置的工具包,那么下面就简单记录一下numpy.random常用的函数,需要的朋友可以参考下
    2023-08-08
  • 详解如何在Python中替换文件路径和要读取的行号

    详解如何在Python中替换文件路径和要读取的行号

    这篇文章主要为大家详细介绍了如何在Python中替换文件路径和要读取的行号,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下
    2007-02-02

最新评论