473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

duplicate and rename a folder and its contents

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.

2 2682
bvdet
2,851 Expert Mod 2GB
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
Eyy, thank you so much, the example helped a lot!! :)
Feb 24 '11 #3

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

Similar topics

0
by: John Dalberg | last post by:
How can I enable "list folder contents" ntfs permission only programmatically? The docs for cacls and xcalcs do not have an example to set this up without setting up the other read permissions. ...
2
by: Ann | last post by:
Hi, Is there a way i can access the contents of a folder from xml and print it out ? (For eg:- some thing like Filesystemobject in vb ) Any help will be appreciated.. Thanks, Ann
3
by: scoop_77 | last post by:
I'm looking for some software that would allow me to point to a folder in Windows Explorer, and have it output an html file with hyperlinks to all of the files within that folder and all of the...
1
by: Stephen | last post by:
Hi, Is there a sample application using ASP .NET to synchronize folder contents? I want to synchronize folder contents on 2 different machines Thanks, Stephen
1
by: jon.ingram | last post by:
I need some vb code that I could perhaps run from access that will look in a particular folder I could specify in the code, and convert all the files (just excel are saved there) to .csv. Is...
2
by: ankita | last post by:
Hello, i am using Asp.net application to read contents of FTP folder. I just want to have names of all folders/files that are inside FTP directory. So for that i don't want to download any...
1
by: tula123 | last post by:
Hi - I'm new to forums so I hope I'm going about this the right way... I wrote a 'quick' VB program to print the filenames within a folder. After the Print Dialog Box comes up and you select your...
1
by: Jim in Arizona | last post by:
Anyone know a way or a document that shows how to display the contents (word & excel docs, etc) of a folder as links on a web form that link to the document? TIA, Jim
3
by: rn5a | last post by:
An inquisitive question...... A ListBox lists all the directories & files residing in a directory on the server. Assume that the ListBox lists 2 directories & 4 files. Also assume that one of...
7
by: devnew | last post by:
hi i am trying to create a cache of digitized values of around 100 image files in a folder..In my program i would like to know from time to time if a new image has been added or removed from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.