簡體   English   中英

如何將一個擴展名的一個目錄的所有文件合並到一個文件夾中

[英]how to combine all the files of one directory of one extension into one folder

如何將一個目錄的所有 PDF 文件(此 pdf 文件可以位於目錄的不同深度)合並到一個新文件夾中?

我試過這個:

new_root = r'C:\Users\me\new_root'
root_with_files = r'C:\Users\me\all_of_my_pdf_files\'

for root, dirs, files in os.walk(root_with_files):
    for file in files:
        os.path.join(new_root, file)

但它不會向我的文件夾添加任何內容

你可以試試這個:

import shutil

new_root = r'C:\Users\me\new_root'
root_with_files = r'C:\Users\me\all_of_my_pdf_files'

for root, dirs, files in os.walk(root_with_files):
    for file in files:
        if file.lower().endswith('.pdf') :  # .pdf files only
            shutil.copy( os.path.join(root, file), new_root )

您的代碼不會將任何文件移動到新文件夾。 您可以使用os.replace(src,dst)移動文件。

嘗試這個:

new_root = r'C:\Users\me\new_root'
root_with_files = r'C:\Users\me\all_of_my_pdf_files\'

for root, dirs, files in os.walk(root_with_files):
    for file in files:
        os.replace(os.path.join(root, file),os.path.join(new_root, file))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM