使用Python編寫(xiě)一個(gè)簡(jiǎn)單的自動(dòng)整理文件腳本
用 Python 寫(xiě)一個(gè)自動(dòng)整理文件的腳本很簡(jiǎn)單,核心思路是:按文件后綴(如 .jpg、.pdf)將文件分類(lèi),移動(dòng)到對(duì)應(yīng)的文件夾(如「圖片」「文檔」)中。以下是一個(gè)實(shí)用的實(shí)現(xiàn)方案,新手也能輕松修改和使用。
腳本功能
將指定文件夾(比如「下載」文件夾)中的所有文件,按類(lèi)型自動(dòng)分類(lèi)到子文件夾中,例如:
- 圖片(.jpg, .png, .gif 等)→
圖片/ - 文檔(.pdf, .docx, .xlsx 等)→
文檔/ - 視頻(.mp4, .avi 等)→
視頻/ - 其他未定義類(lèi)型 →
其他文件/
實(shí)現(xiàn)代碼
import os
import shutil
def organize_files(target_dir):
# 定義文件類(lèi)型與對(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()
# 確定文件所屬類(lèi)別
category = '其他文件' # 默認(rèn)類(lèi)別
for cat, exts in file_categories.items():
if file_ext in exts:
category = cat
break
# 創(chuàng)建對(duì)應(yīng)的類(lèi)別文件夾(如果不存在)
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\你的用戶(hù)名\Downloads"
# Mac/Linux示例:"/Users/你的用戶(hù)名/Downloads"
target_directory = r"C:\Users\你的用戶(hù)名\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\你的用戶(hù)名\Downloads"(注意前面的r不能少) - Mac/Linux 路徑格式:
"/Users/你的用戶(hù)名/Downloads"
運(yùn)行腳本:保存為 file_organizer.py,在終端執(zhí)行 python file_organizer.py。
自定義擴(kuò)展
添加更多類(lèi)型:在 file_categories 字典中添加新的類(lèi)別和對(duì)應(yīng)的后綴,例如:'電子書(shū)': ['.epub', '.mobi']
修改文件夾名稱(chēng):直接修改字典的鍵(如將「圖片」改為「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ò)已存在的分類(lèi)文件夾(如已有的「圖片」文件夾),不會(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)文章
Python自動(dòng)連接SSH的實(shí)現(xiàn)步驟
本文主要介紹了Python自動(dòng)連接SSH的實(shí)現(xiàn)步驟,可以使用paramiko模塊來(lái)編寫(xiě)腳本自動(dòng)執(zhí)行SSH命令,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python辦公自動(dòng)化之發(fā)送電子郵件和Outlook集成
Python辦公?動(dòng)化是利?Python編程語(yǔ)?來(lái)創(chuàng)建腳本和程序,以簡(jiǎn)化、加速和?動(dòng)化?常辦公任務(wù)和?作流程的過(guò)程,本文主要介紹一下如何利用Python實(shí)現(xiàn)發(fā)送電子郵件和Outlook集成,需要的可以參考下2023-12-12
解決windows上安裝tensorflow時(shí)報(bào)錯(cuò),“DLL load failed: 找不到指定的模塊”的問(wèn)題
這篇文章主要介紹了解決windows上安裝tensorflow時(shí)報(bào)錯(cuò),“DLL load failed: 找不到指定的模塊”的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
python人工智能深度學(xué)習(xí)算法優(yōu)化
這篇文章主要為大家介紹了python人工智能深度學(xué)習(xí)關(guān)于算法優(yōu)化詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Python3.7安裝keras和TensorFlow的教程圖解
這篇文章主要介紹了Python3.7安裝keras和TensorFlow經(jīng)驗(yàn),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
熵值法原理及Python實(shí)現(xiàn)的示例詳解
熵值法也稱(chēng)熵權(quán)法,是學(xué)術(shù)研究及實(shí)際應(yīng)用中的一種常用且有效的編制指標(biāo)的方法。本文就來(lái)和大家聊聊熵值法原理及Python實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02

