----------------------使用副檔名分類-----------------------------------------
某天工作的時候
點開了四年多來工作用的一個資料夾
發現裡面的檔案真的是亂到
看到就對工作提不起勁
所以就趁著下班時間
寫了一個小程式
能夠依照副檔名
逐一建立資料夾
並且把檔案丟進去
-----------------------------以下是我的程式碼-----------------------------------------------------
import os
import shutil
file_dir = os.getcwd()
file_cat = {}
for files in os.listdir(file_dir):
if os.path.isfile(files):
file_ext = os.path.splitext(files)[1].lower()
file_namecom = os.path.splitext(files)[0]+ os.path.splitext(files)[1].lower()
file_name = os.path.splitext(files)[0]
file_extremove = file_ext.replace('.', '')
if file_extremove not in file_cat:
# file_cat.get(f'{file_extremove}檔', file_ext)
file_cat[f'{file_extremove}檔'] = f'{file_ext}'
for file in file_cat.keys():
file_path = os.path.join(file_dir, file)
if not os.path.exists(file_path):
os.mkdir(file_path)
for files in os.listdir(file_dir):
if os.path.isfile(files):
file_ext = os.path.splitext(files)[1].lower()
for folder_name , file_extn in file_cat.items():
filext_path = os.path.join(file_dir, files)
folder_path = os.path.join(file_dir, folder_name)
if files =='2.py': #2是我的python程式的名稱 可以自行更改
os.chdir(file_dir) #改變路徑(原本的path)
elif file_ext in file_extn:
shutil.move(filext_path, folder_path)
---------------------------------程式碼到這---------------------------------------------
因為這只是我的興趣
並非專業
還在學習如何寫到很精省