sfortney
sfortney

Reputation: 2123

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?

Upvotes: 5

Views: 13135

Answers (3)

shivam mehta
shivam mehta

Reputation: 31

You can use it to move all subfolders from the same to a different directory to wherever you want.

import shutil
import os
path=r'* Your Path*'
arr = os.listdir(path)
for i in range(len(arr)):
  source_dir=path+'/'+arr[i]
  target_dir = r'*Target path*'
    
  file_names = os.listdir(source_dir)
    
  for file_name in file_names:
      shutil.move(os.path.join(source_dir, file_name), target_dir)

Upvotes: 1

moshiurcse
moshiurcse

Reputation: 33

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)

Upvotes: 3

LampPost
LampPost

Reputation: 866

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.

Upvotes: 13

Related Questions