Search code examples
pythoncsvglobshutil

Loop over multiple folders from list with glob.glob


How can I loop over a defined list of folders and all of the individual files inside each of those folders?

I'm trying to have it copy all the months in each year folder. But when I run it nothing happens..

import shutil
import glob


P4_destdir = ('Z:/Source P4')

yearlist = ['2014','2015','2016']

for year in yearlist:
    for file in glob.glob(r'{0}/*.csv'.format(yearlist)):
        print (file)
        shutil.copy2(file,P4_destdir)

Solution

  • I think the problem might be that you require a / in you source path:

    import shutil
    import glob
    
    
    P4_destdir = ('Z:/Source P4/')
    
    yearlist = ['2014','2015','2016'] # assuming these files are in the same directory as your code.
    
    for year in yearlist:
        for file in glob.glob(r'{0}/*.csv'.format(yearlist)):
            print (file)
            shutil.copy2(file,P4_destdir)
    

    Another thing that might be a problem is if the destination file does not yet exist. You can create it using os.mkdir:

    import os
    
    dest = os.path.isdir('Z:/Source P4/') # Tests if file exists
    if not dest:
        os.mkdir('Z:/Source P4/')