Skip to content

Instantly share code, notes, and snippets.

@thedanhub
Created November 23, 2021 17:55
Show Gist options
  • Save thedanhub/001c0f3263e2cabd60ebc188bd34f9ce to your computer and use it in GitHub Desktop.
Save thedanhub/001c0f3263e2cabd60ebc188bd34f9ce to your computer and use it in GitHub Desktop.
List all files of a specified file type inside a directory and its subdirectories, and copy them to a separate folder with no subdirectories
import os
import shutil
folder_path = '/path/to/source/folder/'
export_path = '/path/to/export/folder'
number_of_files = 0
for dirs, subdirs, files in os.walk(folder_path):
number_of_files = number_of_files+len(files)
for file in files:
if file.endswith(".pdf"):
print(file)
filepath = dirs + os.sep + file
print("Copying " + filepath)
shutil.copy2(filepath, export_path)
print('-----------')
print("Exported " + str(number_of_files) + " files to " + export_path + ".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment