0

how can i combine all PDF files of one directory (this pdfs can be on different deep of directory) into one new folder?

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

germanjke
  • 529
  • 1
  • 5
  • 13
  • I think join doesn't work like that. Maybe you should try the function from [this post](https://stackoverflow.com/questions/3444645/merge-pdf-files) – Charalamm Jun 04 '20 at 07:06

2 Answers2

1

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 )
lenik
  • 23,228
  • 4
  • 34
  • 43
0

Your code doesn't move any files to new folder. you can move your files using 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))
Danyang
  • 26
  • 4