简体   繁体   中英

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 eg ['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 eg 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) 

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM