Friday, August 19, 2022

Copy a file to multiple directories

 Lets say we got nested folders as seen below where we want copy a file (text file in this case) into all the folders.

The python script below does what was stated above.

import os
import glob
import shutil
txt_file = r"C:\Users\Yusuf_08039508010\Documents\...\PDF Others\textfile.txt"
fldr = r'C:\Users\Yusuf_08039508010\Documents\Jupyter_Notebook\2022\...\PDF Others'

# Change dir to parent directory and get all subfolder...
list_of_folder = glob.glob('**/', recursive=True)

# Copy text file from source (src) to destination (dst)
for f in list_of_folder:
    print('Processing...', f)
    shutil.copy( txt_file, f ) # shutil.copy( src, dst )


No comments:

Post a Comment