您好,登錄后才能下訂單哦!
本篇內容主要講解“基于Python怎么實現文件分類器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于Python怎么實現文件分類器”吧!
通過自定義需要整理的文件目錄,將該目錄下面的全部文件按照文件格式完成分類操作。
實現邏輯使用的python技術棧就是os、glob、shutil三個標準庫的綜合運用,完成自動化的文件整理。
分別將這三個文件處理模塊導入代碼塊中,進入后續的開發操作。
# It imports the os module. import os # Shutil is a module that provides a number of high-level operations on files and collections of files. import shutil # The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, # although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed # with [] will be correctly matched. import glob import sys
將需要分類的文件目錄uncatched_dir以及分類后文件存放目錄target_dir設置為可以手動輸入的方式。
# Asking the user to input the path of the directory that contains the files to be sorted. uncatched_dir = input('請輸入待分類的文件路徑:\n') # It checks if the uncatched_dir is empty. if uncatched_dir.strip() == '': print('待分類的文件夾路徑不能為空!') sys.exit() # Asking the user to input the path of the directory that contains the files to be sorted. target_dir = input('請輸入分類后文件存放的目標路徑:\n') # It checks if the target_dir is empty. if target_dir.strip() == '': print('分類后的文件存放路徑不能為空!') sys.exit()
檢驗輸入的分類后文件存放目錄路徑是否存在,因為很可能是輸入一個新的路徑,不存在時則新建一個該路徑。
# It checks if the target_dir exists. If it does not exist, it creates a new directory in the current working directory. if not os.path.exists(target_dir): # It creates a new directory in the current working directory. os.mkdir(target_dir)
定義一個文件移動數量的變量file_move_num,以及一個新建的文件夾數量的變量dir_new_num用于記錄文件整理的結果記錄。
# A variable that is used to count the number of files that have been moved. file_move_num = 0 # A variable that is used to count the number of new directories that have been created. dir_new_num = 0
遍歷需要整理的文件夾目錄uncatched_dir,對該目錄下面的所有類型的文件進行自動整理操作。
# A for loop that iterates through all the files in the uncatched_dir directory. for file_ in glob.glob(f'{uncatched_dir}/**/*', recursive=True): # It checks if the file is a file. if os.path.isfile(file_): # It gets the file name of the file. file_name = os.path.basename(file_) # Checking if the file name contains a period. if '.' in file_name: # Getting the suffix of the file. suffix_name = file_name.split('.')[-1] else: # Used to classify files that do not have a suffix. suffix_name = 'others' # It checks if the directory exists. If it does not exist, it creates a new directory in the current working # directory. if not os.path.exists(f'{target_dir}/{suffix_name}'): # It creates a new directory in the current working directory. os.mkdir(f'{target_dir}/{suffix_name}') # Adding 1 to the variable dir_new_num. dir_new_num += 1 # It copies the file to the target directory. shutil.copy(file_, f'{target_dir}/{suffix_name}') # Adding 1 to the variable file_move_num. file_move_num += 1
注意:為了避免移動文件夾而造成的異常,尤其是系統盤,因此這里用的是復制,也就是shutil.copy函數使用。
最后,將文件分類數量、文件夾新建數量使用print函數進行打印即可。
print(f'整理完成,有{file_move_num}個文件分類到了{dir_new_num}個文件夾中!\n') input('輸入任意鍵關閉窗口...')
為了避免程序執行完成后直接將命令窗口關閉,上面使用了input函數來保持窗口暫停的效果。
到此,相信大家對“基于Python怎么實現文件分類器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。