Python批量查找包含多個(gè)關(guān)鍵詞的PDF文件
在信息爆炸的時(shí)代,數(shù)據(jù)管理變得愈發(fā)重要。U盤(pán)作為一種便攜式存儲(chǔ)設(shè)備,常常承載著我們大量的個(gè)人和工作數(shù)據(jù)。然而,隨著文件數(shù)量的增加,在U盤(pán)中快速找到特定文件常常成為一個(gè)令人頭疼的難題。我們通??梢圆捎胑verything來(lái)快速查找我們想要的文件,但是everything只有查找功能,并沒(méi)有復(fù)制功能,同時(shí)不能進(jìn)行批量的查找,所以這時(shí)就可能要使用到萬(wàn)能的Python工具了,Python中的os標(biāo)準(zhǔn)模塊可以遍歷,查找文件,再用shutil來(lái)文件拷貝到指定位置。
一、查找包含單一關(guān)鍵詞的文件
比如我們要查找U盤(pán)中包括:"翻譯", "國(guó)際", "英語(yǔ)", "國(guó)別" 任一關(guān)鍵詞的pdf文件,找到后復(fù)制到查找文件這一個(gè)目錄下,如果找不到就重新建立查找文件這個(gè)文件夾。代碼如下:
import os import shutil # 定義要查找的關(guān)鍵詞 keywords = ["翻譯", "國(guó)際", "英語(yǔ)", "國(guó)別"] # 定義要遍歷的目錄和目標(biāo)目錄 source_directory = "你的源目錄路徑" # 替換為你的U盤(pán)路徑,如果是當(dāng)前目錄下可以直接填寫(xiě)為"." target_directory = os.path.join(source_directory, "查找文件") # 如果目標(biāo)目錄不存在,則創(chuàng)建它 if not os.path.exists(target_directory): os.makedirs(target_directory) # 遍歷源目錄下的所有文件和文件夾 for root, dirs, files in os.walk(source_directory): for file in files: # 檢查文件名是否包含任何關(guān)鍵詞,并且是PDF文件 if any(keyword in file for keyword in keywords) and file.endswith('.pdf'): source_file_path = os.path.join(root, file) target_file_path = os.path.join(target_directory, file) # 復(fù)制文件到目標(biāo)目錄 shutil.copy(source_file_path, target_file_path) print(f"已復(fù)制: {source_file_path} 到 {target_file_path}") print("文件查找和復(fù)制完成!")
使用時(shí),將 source_directory 替換為你要遍歷的目錄的路徑。
運(yùn)行代碼,它會(huì)遍歷指定的目錄,查找包含關(guān)鍵詞的PDF文件并復(fù)制到“查找文件”文件夾中。
確保在你的Python環(huán)境中安裝了所需的模塊(如 os 和 shutil),這些模塊通常是Python標(biāo)準(zhǔn)庫(kù)的一部分,無(wú)需額外安裝。
二、查找多關(guān)鍵詞同時(shí)出現(xiàn)的文件
查找同時(shí)包含“翻譯”和“碩”兩個(gè)關(guān)鍵詞的PDF文件名,并將其復(fù)制到“查找文件”文件夾中呢?
import os import shutil # 定義要查找的關(guān)鍵詞 keywords = ["翻譯", "碩"] # 定義要遍歷的目錄和目標(biāo)目錄 source_directory = "你的源目錄路徑" # 替換為你的源目錄路徑 target_directory = os.path.join(source_directory, "查找文件") # 如果目標(biāo)目錄不存在,則創(chuàng)建它 if not os.path.exists(target_directory): os.makedirs(target_directory) # 遍歷源目錄下的所有文件和文件夾 for root, dirs, files in os.walk(source_directory): for file in files: # 檢查文件名是否同時(shí)包含所有關(guān)鍵詞,并且是PDF文件 if all(keyword in file for keyword in keywords) and file.endswith('.pdf'): source_file_path = os.path.join(root, file) target_file_path = os.path.join(target_directory, file) # 復(fù)制文件到目標(biāo)目錄 shutil.copy(source_file_path, target_file_path) print(f"已復(fù)制: {source_file_path} 到 {target_file_path}") print("文件查找和復(fù)制完成!")
使用說(shuō)明:
將 source_directory 替換為你要遍歷的目錄的路徑。
運(yùn)行代碼,它會(huì)查找同時(shí)包含“翻譯”和“碩”的PDF文件并將其復(fù)制到“查找文件”文件夾中。
三、把以上兩種功能合二為一
設(shè)置選項(xiàng),當(dāng)用戶輸入不同的選項(xiàng)就進(jìn)行不同的操作。
import os import shutil def find_files_any(source_directory, keywords): target_directory = os.path.join(source_directory, "查找文件_any") if not os.path.exists(target_directory): os.makedirs(target_directory) for root, dirs, files in os.walk(source_directory): for file in files: if any(keyword in file for keyword in keywords): print(f"找到任一關(guān)鍵詞文件: {file}") def find_files_all(source_directory, keywords): target_directory = os.path.join(source_directory, "查找文件_all") if not os.path.exists(target_directory): os.makedirs(target_directory) for root, dirs, files in os.walk(source_directory): for file in files: if all(keyword in file for keyword in keywords): source_file_path = os.path.join(root, file) target_file_path = os.path.join(target_directory, file) shutil.copy(source_file_path, target_file_path) print(f"已復(fù)制: {source_file_path} 到 {target_file_path}") def main(): keywords_any = ["翻譯", "國(guó)際"] keywords_all = ["翻譯", "碩"] print("請(qǐng)選擇查找選項(xiàng):") print("1. 查找任一關(guān)鍵詞") print("2. 查找同時(shí)關(guān)鍵詞") choice = input("請(qǐng)輸入選項(xiàng) (1 或 2): ") source_directory = input("請(qǐng)輸入你的U盤(pán)路徑: ") if choice == "1": find_files_any(source_directory, keywords_any) elif choice == "2": find_files_all(source_directory, keywords_all) else: print("無(wú)效選項(xiàng),請(qǐng)重新運(yùn)行程序。") print("文件查找完成!") if __name__ == "__main__": main()
顯示情況如下:
顯示結(jié)果
四、采用裝飾器法來(lái)寫(xiě)
為了使我們的代碼更pythonic,我們可以設(shè)置一下裝飾器,這樣可以為我們?cè)O(shè)置的函數(shù)添加新的功能。
import os import shutil def choice_decorator(func): def wrapper(keywords): print("請(qǐng)選擇查找選項(xiàng):") print("1. 查找任一關(guān)鍵詞") print("2. 查找同時(shí)關(guān)鍵詞") choice = input("請(qǐng)輸入選項(xiàng) (1 或 2): ") if choice not in ["1", "2"]: print("無(wú)效選項(xiàng),請(qǐng)重新運(yùn)行程序。") return source_directory = input("請(qǐng)輸入你的U盤(pán)路徑: ") if choice == "1": return func(source_directory, keywords[0]) # 傳遞任一關(guān)鍵詞 elif choice == "2": return func(source_directory, keywords[1]) # 傳遞同時(shí)關(guān)鍵詞 return wrapper @choice_decorator def find_files(source_directory, keywords): target_directory = os.path.join(source_directory, f"查找文件_{keywords[0]}") if not os.path.exists(target_directory): os.makedirs(target_directory) for root, dirs, files in os.walk(source_directory): for file in files: if all(keyword in file for keyword in keywords): source_file_path = os.path.join(root, file) target_file_path = os.path.join(target_directory, file) shutil.copy(source_file_path, target_file_path) print(f"已復(fù)制: {source_file_path} 到 {target_file_path}") def main(): keywords_any = ["翻譯", "國(guó)際"] keywords_all = ["翻譯", "碩"] # 將關(guān)鍵詞組合放在一個(gè)列表中,以便裝飾器使用 keywords = [keywords_any, keywords_all] find_files(keywords) print("文件查找完成!") if __name__ == "__main__": main()
五、學(xué)后總結(jié)
本來(lái)是一個(gè)遍歷文件夾進(jìn)行篩選的問(wèn)題,現(xiàn)在可以采用多種方法,分不同的場(chǎng)景進(jìn)行。最后,利用上Python的裝飾器,使我們的程序變得更加高大上。同一個(gè)問(wèn)題,由淺入深,用函數(shù)法、交互法、裝飾器法來(lái)解決,顯示出Python功能的強(qiáng)大和編程時(shí)的靈活性。
到此這篇關(guān)于Python批量查找包含多個(gè)關(guān)鍵詞的PDF文件的文章就介紹到這了,更多相關(guān)Python查找包含多關(guān)鍵詞的PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python程序員開(kāi)發(fā)中常犯的10個(gè)錯(cuò)誤
這篇文章主要介紹了Python程序員開(kāi)發(fā)中常犯的10個(gè)錯(cuò)誤,不知道你有沒(méi)有中槍呢,需要的朋友可以參考下2014-07-07python運(yùn)行cmd命令行的3種方法總結(jié)
雖然python在調(diào)用cmd命令方面使用的比較少,不過(guò)還是要用的,下面這篇文章主要給大家介紹了關(guān)于python運(yùn)行cmd命令行的3種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Python中使用正則表達(dá)式精準(zhǔn)匹配IP地址的案例
Python的正則表達(dá)式(re模塊)是完成這個(gè)任務(wù)的利器,但你知道怎么寫(xiě)才能準(zhǔn)確匹配各種合法的IP地址嗎,今天我們就來(lái)詳細(xì)探討這個(gè)問(wèn)題,感興趣的朋友一起看看吧2025-04-04Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程
Python中我們一般使用SMTP模塊來(lái)首發(fā)郵件,而用email模塊來(lái)處理郵件編碼,本文我們就來(lái)詳細(xì)看一下Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程,需要的朋友可以參考下2016-07-07python操作攝像頭截圖實(shí)現(xiàn)遠(yuǎn)程監(jiān)控的例子
這篇文章主要介紹了python操作攝像頭截圖實(shí)現(xiàn)遠(yuǎn)程監(jiān)控的例子,例子中包含了控制攝像頭、寫(xiě)入Windows注冊(cè)表方法等,需要的朋友可以參考下2014-03-03python中的selenium入門(mén)超詳細(xì)教程
這篇文章主要介紹了python中的selenium入門(mén)超詳細(xì)教程,本文是在python環(huán)境下使用selenium,使用瀏覽器是Chrome,系統(tǒng)是win10系統(tǒng),需要的朋友可以參考下2023-11-11matplotlib繪制鼠標(biāo)的十字光標(biāo)的實(shí)現(xiàn)(自定義方式,官方實(shí)例)
這篇文章主要介紹了matplotlib繪制鼠標(biāo)的十字光標(biāo)(自定義方式,官方實(shí)例),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python實(shí)現(xiàn)人像動(dòng)漫化的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)人像動(dòng)漫化的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05