基于Python實現(xiàn)文件分類器的示例代碼
本文實現(xiàn)文件分類器的目的主要是為了將辦公過程中產(chǎn)生的各種格式的文件完成整理。
通過自定義需要整理的文件目錄,將該目錄下面的全部文件按照文件格式完成分類操作。
實現(xiàn)邏輯使用的python技術(shù)棧就是os、glob、shutil三個標(biāo)準(zhǔn)庫的綜合運用,完成自動化的文件整理。
分別將這三個文件處理模塊導(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è)置為可以手動輸入的方式。
#?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('請輸入分類后文件存放的目標(biāo)路徑:\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用于記錄文件整理的結(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,對該目錄下面的所有類型的文件進(jìn)行自動整理操作。
#?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)盤,因此這里用的是復(fù)制,也就是shutil.copy函數(shù)使用。
最后,將文件分類數(shù)量、文件夾新建數(shù)量使用print函數(shù)進(jìn)行打印即可。
print(f'整理完成,有{file_move_num}個文件分類到了{(lán)dir_new_num}個文件夾中!\n') input('輸入任意鍵關(guān)閉窗口...')
為了避免程序執(zhí)行完成后直接將命令窗口關(guān)閉,上面使用了input函數(shù)來保持窗口暫停的效果。
到此這篇關(guān)于基于Python實現(xiàn)文件分類器的示例代碼的文章就介紹到這了,更多相關(guān)Python文件分類器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python實現(xiàn)Pycharm斷點調(diào)試
這篇文章主要介紹了基于python實現(xiàn)Pycharm斷點調(diào)試,在我們寫程序的時候,很容易遇到各種各樣的bug,然后編譯器提示程序出錯的地方。很多時候可以通過提示的信息修改程序,但是有時我們想得到更多的信息,這個時候就需要進(jìn)行斷點調(diào)試,下面我們就一起來學(xué)習(xí)ycharm斷點調(diào)試2022-02-02python如何實現(xiàn)排序,并標(biāo)上序號
這篇文章主要介紹了python如何實現(xiàn)排序,并標(biāo)上序號,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Pytorch 使用opnecv讀入圖像由HWC轉(zhuǎn)為BCHW格式方式
這篇文章主要介紹了Pytorch 使用opnecv讀入圖像由HWC轉(zhuǎn)為BCHW格式方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06