简体   繁体   English

如何将一个扩展名的一个目录的所有文件合并到一个文件夹中

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

how can i combine all PDF files of one directory (this pdfs can be on different deep of directory) into one new folder?如何将一个目录的所有 PDF 文件(此 pdf 文件可以位于目录的不同深度)合并到一个新文件夹中?

i have been tried this:我试过这个:

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)

but it's doest add anything to my folder但它不会向我的文件夹添加任何内容

You may try this:你可以试试这个:

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 )

Your code doesn't move any files to new folder.您的代码不会将任何文件移动到新文件夹。 you can move your files using os.replace(src,dst) .您可以使用os.replace(src,dst)移动文件。

try this:尝试这个:

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.

相关问题 如何在python中绘制一个文件夹中的所有文件? - How to plot all files in one folder in python? 如何将目录中的所有文件拼接成一个文件 - How to concatenate all files in the directory into one file 将文件夹目录中的所有.csv文件复制到python中的一个文件夹 - Copy all .csv files in a directory of folders to one folder in python 将所有 csv 文件与 pandas 和使用 \ 字符的目录合并到一个文件夹中 - Concatanation of all csv files in one folder with pandas and directory that uses \ character 将文件夹中的所有文件数量合并为一个文件 - Combine every number of files in a folder into one file 根据扩展名将文件合并到一个目录中 - Concat files in one directory based on their extension 如何更改目录和文件夹的所有文件的扩展名 - how to change extension of all the files of a directory and folder 读取一个文件夹中的所有文件后,如何在python中从一个文件夹跳转到另一个文件夹? - How to jump from one folder to another folder in python after reading all the files in one folder? 将文件夹中的文件复制到python中的一个目录中 - Copy files in folder up one directory in python 如何交换具有相同名称但具有不同扩展名的不同文件的一个文件扩展名的文件并维护目录结构? - How to swap files with one file extension with identically named but different files with a different extension and maintain the directory structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM