Mass-Renaming folders win32

E

evaisse

I've a folder structure like this :

"Folder A"
|--> "1"
|--> "2"
---> "3"

"Folder B"
|--> "4"
---> "5"


And I want to obtain a structure like this :

"Folder A - 1"
"Folder A - 2"
"Folder A - 3"
"Folder B - 4"
"Folder B - 5"

Can someone help me ? I am python beginner
 
T

Tim Chase

I've a folder structure like this :
"Folder A"
|--> "1"
|--> "2"
---> "3"

"Folder B"
|--> "4"
---> "5"

And I want to obtain a structure like this :

"Folder A - 1"
"Folder A - 2"
"Folder A - 3"
"Folder B - 4"
"Folder B - 5"

Can someone help me ? I am python beginner

I'd try something like:

import os
from shutil import move

for path, dirs, files in os.walk('.'):
if path <> os.curdir and files:
# the "2:" strips of the ".[os.sep]" at the beginning
newdir = path[2:].replace(os.sep, ' - ')
try:
os.mkdir(newdir)
for f in files:
try:
move(
os.sep.join([path, f]), #the old location
os.sep.join([newdir, f]) #the new location
)
except:
print 'Could not move %s to %s' % (f, newdir)
except:
print 'Could not create %s' % newdir



It doesn't clean up the original directories along the way, but
that may be better, depending on the nesting depth of your
directories...you'd want to make sure that you deal with all the
files within a directory before deleting it.

Feel free to tweak as needed.

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top