Search code examples
pythonpandasglobpathlib

Recursively find and copy files from many folders


I have some files in an array that I want to recursively search from many folders

An example of the filename array is ['A_010720_X.txt','B_120720_Y.txt']

Example of folder structure is as below which I can also provide as an array e.g ['A','B'] and ['2020-07-01','2020-07-12']. The "DL" remains the same for all.

C:\A\2020-07-01\DL C:\B\2020-07-12\DL

etc

I have tried to use shutil but it doesn't seem to work effectively for my requirement as I can only pass in a full file name and not a wildcard. The code I have used with shutil which works but without wildcards and with absolute full file name and path e.g the code below will only give me A_010720_X.txt

I believe the way to go would be using glob or pathlib which i have not used before or cannot find some good examples similar to my use case

    import shutil
    filenames_i_want = ['A_010720_X.txt','B_120720_Y.txt']
    RootDir1 = r'C:\A\2020-07-01\DL'
    TargetFolder = r'C:\ELK\LOGS\ATH\DEST'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
        for name in files:
            if name in filenames_i_want:
                print ("Found")
                SourceFolder = os.path.join(root,name)
                shutil.copy2(SourceFolder, TargetFolder) 

Solution

  • I think this should do what you need assuming they are all .txt files.

    import glob
    import shutil
    
    filenames_i_want = ['A_010720_X.txt','B_120720_Y.txt']
    TargetFolder = r'C:\ELK\LOGS\ATH\DEST'
    all_files = []
    for directory in ['A', 'B']:
        files = glob.glob('C:\{}\*\DL\*.txt'.format(directory))
        all_files.append(files)
    for file in all_files:
        if file in filenames_i_want:
            shutil.copy2(file, TargetFolder)