By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

duplicate and rename a folder and its contents

P: 4
Hi all!
I'm having trouble on doing the following task..
I have a folder, in it, other subFolders witch one with their own list of files (example: ".ma", ".tif", ".jpg", etc.).

So I need to duplicate this folder, rename it, and then rename all the files inside it (the only thing not need to rename are the subFolders).

So I've been trying with "shutil" and with "commands.getstatusoutput" but dont realy know where to go...

Any help would be very appreciated, thanks in advance!
Feb 21 '11 #1

✓ answered by bvdet

Look into the os module. os.walk(dir_name) will return a generator object that walks through a directory tree. The generator produces tuples of dirpath, dirnames, filenames. Example:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. dir_name = (os.path.join('X:/', some_dir, some_subdir))
  4.  
  5. a = os.walk(dir_name)
  6.  
  7. for root, dir, file in a:
  8.  
  9.     print "Root directory: %s" % (root)
  10.  
  11.     if dir:
  12.         print "Subdirectories under %s:" % (root)
  13.         dirList = map(lambda x: '%s\n' % (x), dir)
  14.         dirStr = "".join(dirList)
  15.         print dirStr
  16.     else:
  17.         print "There are no subdirectories under directory %s\n" % (root)
  18.  
  19.     if file:
  20.         print "Files in directory %s:" % (root)
  21.         fileList = map(lambda x: '%s\n' % (os.path.join(root, x)), file)
  22.         fileStr = "".join(fileList)
  23.         print fileStr
  24.     else:
  25.         print "There are no files in directory %s\n" % (root)
You can use this generator to reconstruct the directory and its subdirectories and files using os.mkdir() or os.mkdirs() and shutil functions.

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Look into the os module. os.walk(dir_name) will return a generator object that walks through a directory tree. The generator produces tuples of dirpath, dirnames, filenames. Example:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. dir_name = (os.path.join('X:/', some_dir, some_subdir))
  4.  
  5. a = os.walk(dir_name)
  6.  
  7. for root, dir, file in a:
  8.  
  9.     print "Root directory: %s" % (root)
  10.  
  11.     if dir:
  12.         print "Subdirectories under %s:" % (root)
  13.         dirList = map(lambda x: '%s\n' % (x), dir)
  14.         dirStr = "".join(dirList)
  15.         print dirStr
  16.     else:
  17.         print "There are no subdirectories under directory %s\n" % (root)
  18.  
  19.     if file:
  20.         print "Files in directory %s:" % (root)
  21.         fileList = map(lambda x: '%s\n' % (os.path.join(root, x)), file)
  22.         fileStr = "".join(fileList)
  23.         print fileStr
  24.     else:
  25.         print "There are no files in directory %s\n" % (root)
You can use this generator to reconstruct the directory and its subdirectories and files using os.mkdir() or os.mkdirs() and shutil functions.
Feb 23 '11 #2

P: 4
Eyy, thank you so much, the example helped a lot!! :)
Feb 24 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.