基于Python實現(xiàn)文件分類器的示例代碼
本文實現(xiàn)文件分類器的目的主要是為了將辦公過程中產生的各種格式的文件完成整理。
通過自定義需要整理的文件目錄,將該目錄下面的全部文件按照文件格式完成分類操作。
實現(xiàn)邏輯使用的python技術棧就是os、glob、shutil三個標準庫的綜合運用,完成自動化的文件整理。
分別將這三個文件處理模塊導入代碼塊中,進入后續(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設置為可以手動輸入的方式。
#?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)
定義一個文件移動數(shù)量的變量file_move_num,以及一個新建的文件夾數(shù)量的變量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
注意:為了避免移動文件夾而造成的異常,尤其是系統(tǒng)盤,因此這里用的是復制,也就是shutil.copy函數(shù)使用。
最后,將文件分類數(shù)量、文件夾新建數(shù)量使用print函數(shù)進行打印即可。
print(f'整理完成,有{file_move_num}個文件分類到了{dir_new_num}個文件夾中!\n') input('輸入任意鍵關閉窗口...')
為了避免程序執(zhí)行完成后直接將命令窗口關閉,上面使用了input函數(shù)來保持窗口暫停的效果。
到此這篇關于基于Python實現(xiàn)文件分類器的示例代碼的文章就介紹到這了,更多相關Python文件分類器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Pytorch 使用opnecv讀入圖像由HWC轉為BCHW格式方式
這篇文章主要介紹了Pytorch 使用opnecv讀入圖像由HWC轉為BCHW格式方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06