您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“基于Python怎么實(shí)現(xiàn)文件分類器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“基于Python怎么實(shí)現(xiàn)文件分類器”吧!
通過自定義需要整理的文件目錄,將該目錄下面的全部文件按照文件格式完成分類操作。
實(shí)現(xiàn)邏輯使用的python技術(shù)棧就是os、glob、shutil三個(gè)標(biāo)準(zhǔn)庫的綜合運(yùn)用,完成自動(dòng)化的文件整理。
分別將這三個(gè)文件處理模塊導(dǎo)入代碼塊中,進(jìn)入后續(xù)的開發(fā)操作。
# 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設(shè)置為可以手動(dòng)輸入的方式。
# Asking the user to input the path of the directory that contains the files to be sorted. uncatched_dir = input('請(qǐng)輸入待分類的文件路徑:\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('請(qǐng)輸入分類后文件存放的目標(biāo)路徑:\n') # It checks if the target_dir is empty. if target_dir.strip() == '': print('分類后的文件存放路徑不能為空!') sys.exit()
檢驗(yàn)輸入的分類后文件存放目錄路徑是否存在,因?yàn)楹芸赡苁禽斎胍粋€(gè)新的路徑,不存在時(shí)則新建一個(gè)該路徑。
# 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)
定義一個(gè)文件移動(dòng)數(shù)量的變量file_move_num,以及一個(gè)新建的文件夾數(shù)量的變量dir_new_num用于記錄文件整理的結(jié)果記錄。
# 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,對(duì)該目錄下面的所有類型的文件進(jìn)行自動(dòng)整理操作。
# 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
注意:為了避免移動(dòng)文件夾而造成的異常,尤其是系統(tǒng)盤,因此這里用的是復(fù)制,也就是shutil.copy函數(shù)使用。
最后,將文件分類數(shù)量、文件夾新建數(shù)量使用print函數(shù)進(jìn)行打印即可。
print(f'整理完成,有{file_move_num}個(gè)文件分類到了{(lán)dir_new_num}個(gè)文件夾中!\n') input('輸入任意鍵關(guān)閉窗口...')
為了避免程序執(zhí)行完成后直接將命令窗口關(guān)閉,上面使用了input函數(shù)來保持窗口暫停的效果。
到此,相信大家對(duì)“基于Python怎么實(shí)現(xiàn)文件分類器”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。