[英]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.