Suresh Subramaniam
Suresh Subramaniam

Reputation: 57

Copy files from Windows folder tree to another folder

I have a Windows folder structure and files, something like below

I want to copy all files to a destination folder like below

I tried using glob.glob() to get list of files in the source tree structure but am not able copy them to destination. I tried shutils.copytree() but it copies the tree structure also and I want the destinationfolder to be flat. I tried os.walk() but it also does not work for me. Or I don't know how to set the parameters for the above functions to make them work.

Any help would be highly appreciated.

Thanks

Upvotes: 0

Views: 121

Answers (1)

cerofrais
cerofrais

Reputation: 1337

try looping through the list you got from glob.glob and use shutil.copy() syntax for shutil is shutil.copy(src,dest)

you have to make a destination folder and destination path like (foldername/filename.txt)

import os
import shutil

    for f in list(glob.glob()):
        destpath = os.path.join('c:\\dest',f.split(\\)[-1])
        shutil.copy(f,destpath)

Upvotes: 0

Related Questions