Skip to content

Instantly share code, notes, and snippets.

@abeldantas
Last active January 1, 2024 14:38
Show Gist options
  • Save abeldantas/32684f2030a6ac7038dfe2a0e7b2f2ee to your computer and use it in GitHub Desktop.
Save abeldantas/32684f2030a6ac7038dfe2a0e7b2f2ee to your computer and use it in GitHub Desktop.
Copy files with extensions nested in path
"""
Nested Extension Copier: A Python script to recursively copy files with specified extensions
from a source directory to a destination directory. It accepts multiple extensions as command-line
arguments, allowing for flexible and targeted file copying. Ideal for organizing files or selective backups.
"""
import os
import shutil
import sys
def copy_specific_files(src_path, dest_path, extensions):
if not os.path.exists(dest_path):
os.makedirs(dest_path)
for root, dirs, files in os.walk(src_path):
for file in files:
if os.path.splitext(file)[1].lower() in extensions:
shutil.copy2(os.path.join(root, file), dest_path)
def main():
if len(sys.argv) < 4:
print("Usage: python nested_extension_copier.py [source_path] [destination_path] [extensions...]")
return
src_path = sys.argv[1]
dest_path = sys.argv[2]
extensions = set(sys.argv[3:])
copy_specific_files(src_path, dest_path, extensions)
print(f"Files copied from {src_path} to {dest_path}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment