Robert Steckroth
unread,Oct 8, 2012, 10:26:57 PM10/8/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
Hello Gang, while creating a simple loop to copy a directory
structure, I encountered a strange error
with os.path.join. In the loop below, only the first iteration of the
loop will join the new_dir with no_base.
After hours of research, I still cannot pin this one down. I have it
working at the bottom, but it does not feel as safe.
Has anyone seen this before?
original_dir = '/work/sites/templates'
new_dir = '/work/sites/copy_of_structure'
for (path, dirs, files) in os.walk(original_dir):
no_base = path.replace(original_dir, '')
new_path = os.path.join(new_dir, no_base)
self.stdout.write('path: %s \n' % new_path)
output:
path: /work/sites/copy_of_structure/
path: /gggg
path: /gggg/hdbklasj
should be:
path: /work/sites/copy_of_structure/
path: /work/sites/copy_of_structure/gggg
path: /work/sites/copy_of_structure/gggg/hdbklasj
HOWEVER:
This works just fine:
original_dir = '/work/sites/templates'
new_dir = '/work/sites/copy_of_structure'
for (path, dirs, files) in os.walk(original_dir):
no_base = path.replace(original_dir, 'new_dir')
self.stdout.write('path: %s \n' % new_path)