Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy all files from a folder (including sub-folder) while not copying the folder structure in python

Tags:

python

Can someone help me about how to copy all files from a folder to another destination folder in python. The catch is I do not want to copy the sub-directory structure. But I want the files within them.

For example, lets say in the root folder, there are 3 folders, each containing 10 files. Also in each of them there are 2 folders each containing 5 files. (so each first level folder has in total 20 files and 2 sub directories under it). Bringing the total to 60 files.

I wish to copy all of those 60 files to a single destination directory, discarding the subfolder structure.

This is the code I've tried:

# path : source folder path
# compiled_path: destination folder path
w = os.walk(path)
for root, dirs, files in w:
    for dir_name in dirs:
        file_list_curent_dir = os.walk(path+"\\"+dir_name).next()[2]
        for item in file_list_curent_dir:
            shutil.copy(path+"\\"+dir_name+"\\"+item, compiled_path+"\\"+item )

It copies the files uppermost level, not the folders within sub-directories.

Thank you very much for your time.

like image 671
Quazi Farhan Avatar asked Apr 21 '14 11:04

Quazi Farhan


People also ask

How do I copy an entire folder structure without copying the files?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

How do I copy a file from multiple subfolders to one directory in Python?

move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory.

How do you copy all the contents of a directory including sub directories to another directory?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.

How do I copy a file from multiple sub folders to one folder?

Once the files are visible, press Ctrl-A to select all of them, then drag and drop them to the right location. (If you want to copy the files to another folder on the same drive, remember to hold down Ctrl while you drag and drop; see The many ways to copy, move, or delete multiple files for details.)


1 Answers

import os
import shutil

for root, dirs, files in os.walk('.'):  # replace the . with your starting directory
   for file in files:
      path_file = os.path.join(root,file)
      shutil.copy2(path_file,'destination_directory') # change you destination dir
like image 196
J'e Avatar answered Oct 07 '22 20:10

J'e