最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python編寫(xiě)一個(gè)簡(jiǎn)單的自動(dòng)整理文件腳本

 更新時(shí)間:2025年12月19日 09:16:10   作者:Andrew-國(guó)星宇航  
這篇文章主要為大家詳細(xì)介紹了如何使用Python編寫(xiě)一個(gè)簡(jiǎn)單的自動(dòng)整理文件腳本,可以按文件后綴將文件分類,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

用 Python 寫(xiě)一個(gè)自動(dòng)整理文件的腳本很簡(jiǎn)單,核心思路是:按文件后綴(如 .jpg、.pdf)將文件分類,移動(dòng)到對(duì)應(yīng)的文件夾(如「圖片」「文檔」)中。以下是一個(gè)實(shí)用的實(shí)現(xiàn)方案,新手也能輕松修改和使用。

腳本功能

將指定文件夾(比如「下載」文件夾)中的所有文件,按類型自動(dòng)分類到子文件夾中,例如:

  • 圖片(.jpg, .png, .gif 等)→ 圖片/
  • 文檔(.pdf, .docx, .xlsx 等)→ 文檔/
  • 視頻(.mp4, .avi 等)→ 視頻/
  • 其他未定義類型 → 其他文件/

實(shí)現(xiàn)代碼

import os
import shutil

def organize_files(target_dir):
    # 定義文件類型與對(duì)應(yīng)文件夾的映射(可根據(jù)需求擴(kuò)展)
    file_categories = {
        '圖片': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp'],
        '文檔': ['.pdf', '.docx', '.doc', '.xlsx', '.xls', '.pptx', '.ppt', '.txt', '.md'],
        '視頻': ['.mp4', '.avi', '.mov', '.mkv', '.flv'],
        '音頻': ['.mp3', '.wav', '.flac', '.m4a'],
        '壓縮包': ['.zip', '.rar', '.7z', '.tar', '.gz'],
        '程序': ['.exe', '.msi', '.py', '.java', '.cpp', '.html', '.css', '.js']
    }
    
    # 遍歷目標(biāo)文件夾中的所有項(xiàng)目
    for item in os.listdir(target_dir):
        item_path = os.path.join(target_dir, item)
        
        # 跳過(guò)文件夾(只處理文件)
        if os.path.isdir(item_path):
            continue
        
        # 獲取文件后綴(轉(zhuǎn)為小寫(xiě),避免大小寫(xiě)問(wèn)題)
        file_ext = os.path.splitext(item)[1].lower()
        
        # 確定文件所屬類別
        category = '其他文件'  # 默認(rèn)類別
        for cat, exts in file_categories.items():
            if file_ext in exts:
                category = cat
                break
        
        # 創(chuàng)建對(duì)應(yīng)的類別文件夾(如果不存在)
        category_dir = os.path.join(target_dir, category)
        os.makedirs(category_dir, exist_ok=True)
        
        # 處理同名文件(避免覆蓋)
        target_path = os.path.join(category_dir, item)
        counter = 1
        while os.path.exists(target_path):
            # 例如:test.jpg → test(1).jpg
            name, ext = os.path.splitext(item)
            target_path = os.path.join(category_dir, f"{name}({counter}){ext}")
            counter += 1
        
        # 移動(dòng)文件到目標(biāo)文件夾
        shutil.move(item_path, target_path)
        print(f"已移動(dòng):{item} → {category}/")

if __name__ == "__main__":
    # 替換為你要整理的文件夾路徑(注意斜杠方向)
    # Windows示例:r"C:\Users\你的用戶名\Downloads"
    # Mac/Linux示例:"/Users/你的用戶名/Downloads"
    target_directory = r"C:\Users\你的用戶名\Downloads"
    
    # 檢查路徑是否存在
    if not os.path.exists(target_directory):
        print(f"錯(cuò)誤:路徑不存在 → {target_directory}")
    else:
        print(f"開(kāi)始整理文件夾:{target_directory}")
        organize_files(target_directory)
        print("整理完成!")

使用方法

修改路徑:將代碼中 target_directory 的值改為你要整理的文件夾路徑(比如下載文件夾)。

  • Windows 路徑格式:r"C:\Users\你的用戶名\Downloads"(注意前面的 r 不能少)
  • Mac/Linux 路徑格式:"/Users/你的用戶名/Downloads"

運(yùn)行腳本:保存為 file_organizer.py,在終端執(zhí)行 python file_organizer.py。

自定義擴(kuò)展

添加更多類型:在 file_categories 字典中添加新的類別和對(duì)應(yīng)的后綴,例如:'電子書(shū)': ['.epub', '.mobi']

修改文件夾名稱:直接修改字典的鍵(如將「圖片」改為「Images」)。

排除特定文件:如果有不想移動(dòng)的文件(如 README.txt),可在遍歷前添加判斷跳過(guò)。

知識(shí)擴(kuò)展

python3 自動(dòng)整理文件

思路:

1.在該文件夾里面創(chuàng)建子文件夾

2.判斷該文件夾里面所有文件的格式,也就是什么后綴名

3.將文件進(jìn)行重命名并放入剛創(chuàng)建好的子文件夾中

4.兩種整理辦法:

(1)利用shutil.copy這個(gè)函數(shù)進(jìn)行復(fù)制到子文件夾中

(2)利用shutil.move這個(gè)函數(shù)進(jìn)行剪切到子文件夾中

實(shí)現(xiàn)代碼

方法一:復(fù)制版代碼

import os
import shutil


lj = 'D:\Desktop\自動(dòng)整理'                                                     #文件夾的路徑

os.mkdir(lj + './視頻')                                                      #創(chuàng)建子文件夾,命名為視頻
sp = lj + './視頻'                                                           #拼接成一個(gè)該子文件夾(視頻)的字符串
sp_path = os.path.abspath(sp)                                               #返回該文件夾的絕對(duì)路徑

os.mkdir(lj + './照片')                                                      #創(chuàng)建子文件夾,命名為照片
zp = lj + './照片'                                                           #拼接成一個(gè)該子文件夾(照片)的字符串
zp_path = os.path.abspath(zp)                                               #返回該文件夾的絕對(duì)路徑

os.mkdir(lj + './音樂(lè)')                                                      #創(chuàng)建子文件夾,命名為照片
music = lj + './音樂(lè)'                                                           #拼接成一個(gè)該子文件夾(照片)的字符串
music_path = os.path.abspath(music)                                               #返回該文件夾的絕對(duì)路徑


i = 1
j = 1
m = 1
for file in os.listdir(lj):                                                  #遍歷文件夾里面的文件:
    if file.endswith('.jpg'):                                                #判斷是不是ipg格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(zp_path + './' + str(i) + '.jpg')          #生成新的照片的絕對(duì)路徑
        shutil.copy(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        i += 1

    if file.endswith('.mp4'):                                                #判斷是不是MP4格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(sp_path + './' + str(j) + '.mp4')          #生成新的照片的絕對(duì)路徑
        shutil.copy(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        j += 1

    if file.endswith('.png'):                                                #判斷是不是png格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(zp_path + './' + str(i) + '.png')          #生成新的照片的絕對(duì)路徑
        shutil.copy(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        i += 1

    if file.endswith('.jpeg'):                                                #判斷是不是jpeg格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(zp_path + './' + str(i) + '.jpeg')          #生成新的照片的絕對(duì)路徑
        shutil.copy(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        i += 1

    if file.endswith('.mp3'):                                                #判斷是不是jpeg格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(music_path + './' + str(m) + '.mp3')          #生成新的照片的絕對(duì)路徑
        shutil.copy(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        m += 1

方法二:剪切版代碼:

import os
import shutil


lj = 'D:\Desktop\自動(dòng)整理'                                                     #文件夾的路徑

os.mkdir(lj + './視頻')                                                      #創(chuàng)建子文件夾,命名為視頻
sp = lj + './視頻'                                                           #拼接成一個(gè)該子文件夾(視頻)的字符串
sp_path = os.path.abspath(sp)                                               #返回該文件夾的絕對(duì)路徑

os.mkdir(lj + './照片')                                                      #創(chuàng)建子文件夾,命名為照片
zp = lj + './照片'                                                           #拼接成一個(gè)該子文件夾(照片)的字符串
zp_path = os.path.abspath(zp)                                               #返回該文件夾的絕對(duì)路徑

os.mkdir(lj + './音樂(lè)')                                                      #創(chuàng)建子文件夾,命名為照片
music = lj + './音樂(lè)'                                                        #拼接成一個(gè)該子文件夾(照片)的字符串
music_path = os.path.abspath(music)                                         #返回該文件夾的絕對(duì)路徑


i = 1
j = 1
m = 1
for file in os.listdir(lj):                                                  #遍歷文件夾里面的文件:
    if file.endswith('.jpg'):                                                #判斷是不是ipg格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(zp_path + './' + str(i) + '.jpg')          #生成新的照片的絕對(duì)路徑
        shutil.move(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        i += 1

    if file.endswith('.mp4'):                                                #判斷是不是MP4格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(sp_path + './' + str(j) + '.mp4')          #生成新的照片的絕對(duì)路徑
        shutil.move(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        j += 1

    if file.endswith('.png'):                                                #判斷是不是png格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(zp_path + './' + str(i) + '.png')          #生成新的照片的絕對(duì)路徑
        shutil.move(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        i += 1

    if file.endswith('.jpeg'):                                                #判斷是不是jpeg格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(zp_path + './' + str(i) + '.jpeg')          #生成新的照片的絕對(duì)路徑
        shutil.move(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        i += 1

    if file.endswith('.mp3'):                                                #判斷是不是jpeg格式的照片
        oldpath = os.path.abspath(lj + './' + str(file))                     #生成舊的照片的絕對(duì)路徑
        newpath = os.path.abspath(music_path + './' + str(m) + '.mp3')          #生成新的照片的絕對(duì)路徑
        shutil.move(os.path.abspath(oldpath), os.path.abspath(newpath))      #復(fù)制到新的文件夾里
        m += 1

其實(shí)這兩個(gè)代碼都是大同小異,只是把shutil.copy變成了shutil.move,其他都是一模一樣的

注意事項(xiàng)

  • 首次使用建議先備份文件,避免誤操作。
  • 腳本會(huì)跳過(guò)已存在的分類文件夾(如已有的「圖片」文件夾),不會(huì)遞歸處理子文件夾。
  • 遇到同名文件時(shí),會(huì)自動(dòng)在文件名后加編號(hào)(如 test(1).jpg),避免覆蓋。

用這個(gè)腳本,從此告別雜亂的下載文件夾啦!

到此這篇關(guān)于使用Python編寫(xiě)一個(gè)簡(jiǎn)單的自動(dòng)整理文件腳本的文章就介紹到這了,更多相關(guān)Python自動(dòng)整理文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

永顺县| 镇平县| 南陵县| 延庆县| 延吉市| 驻马店市| 石景山区| 新平| 昌平区| 娄烦县| 西华县| 乌鲁木齐县| 鹤岗市| 诏安县| 称多县| 林西县| 临西县| 乌兰察布市| 龙门县| 桂阳县| 深泽县| 曲松县| 东平县| 临漳县| 十堰市| 陆良县| 青岛市| 富民县| 偃师市| 临沧市| 台中县| 高安市| 郑州市| 海安县| 铜鼓县| 汝城县| 兴仁县| 阿荣旗| 鄄城县| 宁海县| 全州县|