How to copy a file, recreating the directory structure, using python?

To copy “Folder1/Folder2/file1” to “Folder3”


source structure:

Folder1/FolderA ………………….(not to be copied)
Folder1/fileX…………………………(not to be copied)
Folder1/Folder2/file1


desired destination structure:

Folder3/Folder1/Folder2/file1

src = "Folder1/Folder2/file1"
dst = "Folder3"+src
dstfolder = os.path.dirname(dst)
if not os.path.exists(dstfolder):
    os.makedirs(dstfolder)
shutil.copy(src,dst)


P.S. This is a reproduction of my own answer on stackoverflow. https://stackoverflow.com/questions/30856634/how-to-copy-a-file-recreating-the-directory-structure-using-python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s