Reputation: 2755
I get the output of following program as ['file\new.txt','file\test\test.doc',...] How can I copy the files from result keeping the same directory structure.
import re
import shutil
allfileaddr = []
with open("file.cproj", 'r') as fread:
for line in fread:
match = re.search(r'Include',line)
if match:
loc = line.split('"')
allfileaddr.append(loc[1])
print(allfileaddr)
Upvotes: 3
Views: 5003
Reputation: 2098
I am not sure exactly what you mean by same file structure but I am assuming you want to copy the files to a new directory but keep the "/file/test" subdirectory structure.
import re, os, shutil
#directory you wish to copy all the files to.
dst = "c:\path\to\dir"
src = "c:\path\to\src\dir"
with open("file.cproj", 'r') as fread:
for line in fread:
match = re.search(r'Include',line)
if match:
loc = line.split('"')
#concatenates destination and source path with subdirectory path and copies file over.
dst_file_path = "%s\%s" % (dst,loc[1])
(root,file_name) = os.path.splitext(dst_file_path)
# Creates directory if one doesn't exist
if not os.path.isdir(root):
os.makedirs(root)
src_file_path = os.path.normcase("%s/%s" % (src,loc[1]))
shutil.copyfile(src_file_path,dst_file_path)
print dst + loc[1]
This little script will set a designated directory you want to copy all of these files to while keeping the original subdirectory structure intact. Hope this is what you were looking for. If not, let me know and I can tweak it.
Upvotes: 3