Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy files from multiple directories into one directory using Python

What is the easiest way to copy files from multiple directories into just one directory using python? To be more clear, I have a tree that looks like this

+Home_Directory
  ++folder1
   -csv1.csv
   -csv2.csv
  ++folder2
   -csv3.csv
   -csv4.csv

and I want to put csv1,csv2,...etc all into some specified directory without the folder hierarchy.

+some_folder
   -csv1.csv
   -csv2.csv
   -csv3.csv
   -csv4.csv

Some solutions I have looked at:

Using shutil.copytree will not work because it will preserve the file structure which is not what I want.

The code I am playing with is very similar to what is posted in this question: copy multiple files in python the problem is that I do not know how to do this iteratively. Presumably it would just be another for loop on top of this but I am not familiar enough with the os and shutil libraries to know exactly what I am iterating over. Any help on this?

like image 801
sfortney Avatar asked Dec 20 '22 05:12

sfortney


2 Answers

This is what I thought of. I am assuming you are only pulling csv files from 1 directory.

RootDir1 = r'*your directory*'
TargetFolder = r'*your target folder*'
for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
        for name in files:
            if name.endswith('.csv'):
                print "Found"
                SourceFolder = os.path.join(root,name)
                shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder

Edit: missing a ' at the end of RootDir1. You can also use this as a starting guide to make it work as desired.

like image 182
LampPost Avatar answered Dec 21 '22 23:12

LampPost


import glob
import shutil
#import os
dst_dir = "E:/images"
print ('Named explicitly:')
for name in glob.glob('E:/ms/*/*/*'):    
    if name.endswith(".jpg") or name.endswith(".pdf")  : 
        shutil.copy(name, dst_dir)
        print ('\t', name)
like image 42
moshiurcse Avatar answered Dec 21 '22 22:12

moshiurcse