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對(duì)列進(jìn)行平移變換的方法(shift)
今天小編就為大家分享一篇python對(duì)列進(jìn)行平移變換的方法(shift),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python select.select模塊通信全過(guò)程解析
這篇文章主要為大家解析了python select.select模塊通信全過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
python 實(shí)現(xiàn)socket服務(wù)端并發(fā)的四種方式
這篇文章主要介紹了python 實(shí)現(xiàn)socket服務(wù)端并發(fā)的四種方式,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
Python Flask前端自動(dòng)登錄功能實(shí)現(xiàn)詳解
這篇文章主要介紹了Python Flask前端自動(dòng)登錄功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
用python實(shí)現(xiàn)一幅春聯(lián)實(shí)例代碼
大家好,本篇文章主要講的是用python實(shí)現(xiàn)一幅春聯(lián)實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
使用Python編寫(xiě)一個(gè)SQL語(yǔ)句自動(dòng)轉(zhuǎn)換工具(UPDATE到INSERT轉(zhuǎn)換)
在日常數(shù)據(jù)庫(kù)維護(hù)和數(shù)據(jù)處理過(guò)程中,我們經(jīng)常需要將UPDATE語(yǔ)句轉(zhuǎn)換為INSERT語(yǔ)句,特別是在數(shù)據(jù)遷移、備份恢復(fù)或測(cè)試數(shù)據(jù)準(zhǔn)備的場(chǎng)景中,本文將介紹如何使用Python編寫(xiě)一個(gè)自動(dòng)化工具,實(shí)現(xiàn)UPDATE語(yǔ)句到INSERT語(yǔ)句的高效轉(zhuǎn)換,需要的朋友可以參考下2025-09-09
python中的yield from語(yǔ)法快速學(xué)習(xí)
在本篇文章里小編給大家整理的是一篇關(guān)于python中的yield from語(yǔ)法快速學(xué)習(xí)相關(guān)內(nèi)容,有興趣的朋友們可以參考下。2020-11-11
Python使用Selenium模塊模擬瀏覽器抓取斗魚(yú)直播間信息示例
這篇文章主要介紹了Python使用Selenium模塊模擬瀏覽器抓取斗魚(yú)直播間信息,涉及Python基于Selenium模塊的模擬瀏覽器登陸、解析、抓取信息,以及MongoDB數(shù)據(jù)庫(kù)的連接、寫(xiě)入等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

