亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python批量查找包含多個(gè)關(guān)鍵詞的PDF文件

 更新時(shí)間:2024年11月01日 08:34:59   作者:PythonFun  
在信息爆炸的時(shí)代,數(shù)據(jù)管理變得愈發(fā)重要,本文主要為大家介紹了如何通過Python批量查找包含多個(gè)關(guān)鍵詞的PDF文件,希望對(duì)大家有所幫助

在信息爆炸的時(shí)代,數(shù)據(jù)管理變得愈發(fā)重要。U盤作為一種便攜式存儲(chǔ)設(shè)備,常常承載著我們大量的個(gè)人和工作數(shù)據(jù)。然而,隨著文件數(shù)量的增加,在U盤中快速找到特定文件常常成為一個(gè)令人頭疼的難題。我們通常可以采用everything來快速查找我們想要的文件,但是everything只有查找功能,并沒有復(fù)制功能,同時(shí)不能進(jìn)行批量的查找,所以這時(shí)就可能要使用到萬能的Python工具了,Python中的os標(biāo)準(zhǔn)模塊可以遍歷,查找文件,再用shutil來文件拷貝到指定位置。

一、查找包含單一關(guān)鍵詞的文件

比如我們要查找U盤中包括:"翻譯", "國際", "英語", "國別" 任一關(guān)鍵詞的pdf文件,找到后復(fù)制到查找文件這一個(gè)目錄下,如果找不到就重新建立查找文件這個(gè)文件夾。代碼如下:

import os
import shutil
 
# 定義要查找的關(guān)鍵詞
keywords = ["翻譯", "國際", "英語", "國別"]
 
# 定義要遍歷的目錄和目標(biāo)目錄
source_directory = "你的源目錄路徑"  # 替換為你的U盤路徑,如果是當(dāng)前目錄下可以直接填寫為"."
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)庫的一部分,無需額外安裝。

二、查找多關(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ù)制完成!")

使用說明:

將 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 = ["翻譯", "國際"]
    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盤路徑: ")
    
    if choice == "1":
        find_files_any(source_directory, keywords_any)
    elif choice == "2":
        find_files_all(source_directory, keywords_all)
    else:
        print("無效選項(xiàng),請(qǐng)重新運(yùn)行程序。")
 
    print("文件查找完成!")
 
if __name__ == "__main__":
    main()

顯示情況如下:

顯示結(jié)果

四、采用裝飾器法來寫

為了使我們的代碼更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("無效選項(xiàng),請(qǐng)重新運(yùn)行程序。")
            return
        
        source_directory = input("請(qǐng)輸入你的U盤路徑: ")
        
        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 = ["翻譯", "國際"]
    keywords_all = ["翻譯", "碩"]
    
    # 將關(guān)鍵詞組合放在一個(gè)列表中,以便裝飾器使用
    keywords = [keywords_any, keywords_all]
    
    find_files(keywords)
 
    print("文件查找完成!")
 
if __name__ == "__main__":
    main()

五、學(xué)后總結(jié)

本來是一個(gè)遍歷文件夾進(jìn)行篩選的問題,現(xiàn)在可以采用多種方法,分不同的場景進(jìn)行。最后,利用上Python的裝飾器,使我們的程序變得更加高大上。同一個(gè)問題,由淺入深,用函數(shù)法、交互法、裝飾器法來解決,顯示出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)文章

最新評(píng)論