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