亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用python批量移動文件

 更新時間:2021年01月14日 11:11:54   作者:風(fēng)中狂笑  
這篇文章主要介紹了如何用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批量移動文件的詳細(xì)內(nèi)容,更多關(guān)于python批量移動文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python機器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor)實例詳解

    Python機器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor)實例詳解

    這篇文章主要介紹了Python機器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor),結(jié)合實例形式分析了k-近鄰算法的原理、操作步驟、相關(guān)實現(xiàn)與使用技巧,需要的朋友可以參考下
    2018-06-06
  • 利用python實現(xiàn)對web服務(wù)器的目錄探測的方法

    利用python實現(xiàn)對web服務(wù)器的目錄探測的方法

    這篇文章主要介紹了利用python實現(xiàn)對web服務(wù)器的目錄探測的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Python?Tkinter庫從入門到進(jìn)階使用教程

    Python?Tkinter庫從入門到進(jìn)階使用教程

    Tkinter是Python標(biāo)準(zhǔn)庫中內(nèi)置的圖形用戶界面(GUI)工具包,提供了創(chuàng)建窗口、按鈕、文本框等GUI元素的功能,本文將介紹Tkinter的基礎(chǔ)知識,幫助大家快速入門
    2023-12-12
  • Python性能優(yōu)化技巧

    Python性能優(yōu)化技巧

    Python的批評者聲稱Python性能低效、執(zhí)行緩慢,但實際上并非如此:嘗試以下6個小技巧,可以加快Pytho應(yīng)用程序。
    2015-03-03
  • QT5 Designer 打不開的問題及解決方法

    QT5 Designer 打不開的問題及解決方法

    這篇文章主要介紹了QT5 Designer 打不開的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • pyinstaller還原python代碼過程圖解

    pyinstaller還原python代碼過程圖解

    這篇文章主要介紹了pyinstaller還原python代碼過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • python一秒搭建FTP服務(wù)器

    python一秒搭建FTP服務(wù)器

    今天給大家分享一篇教程關(guān)于python一秒搭建FTP服務(wù)器的教程,在搭建過程中需要用到pyftpdlib模塊,對python FTP服務(wù)器搭建過程感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • 使用Django開發(fā)簡單接口實現(xiàn)文章增刪改查

    使用Django開發(fā)簡單接口實現(xiàn)文章增刪改查

    這篇文章主要介紹了使用Django開發(fā)簡單接口實現(xiàn)文章增刪改查,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python prettytable模塊應(yīng)用詳解

    Python prettytable模塊應(yīng)用詳解

    PrettyTable 是python中的一個第三方庫,可用來生成美觀的ASCII格式的表格,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09
  • python3中的函數(shù)與參數(shù)及空值問題

    python3中的函數(shù)與參數(shù)及空值問題

    這篇文章主要介紹了python3-函數(shù)與參數(shù)以及空值,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11

最新評論