[Tutor] Partly OT: order of renaming files and directories
Bernard Lebel
3dbernard at gmail.com
Mon Sep 12 16:30:20 CEST 2005
Hello,
I'm creating a script that will rename directories and files (hence
the regular expression thing I asked about last week). I just wanted
to ask if there is a recommended order to rename stuff, because I want
to avoid any potential problem.
What I mean:
I traverse some areas of the file server with os.walk(). If I want to
rename a directory, is it better that I rename the directory curretly
being visited, or should I iterate the files and directories in the
current directory and rename them instead?
To summarize.
Renaming the current directory:
# Iterate roots
for sRoot in aRoots:
print '\nWalking root %s...\n' % sRoot
# Walk-down root
for sDir, aDirs, aFiles in os.walk( sRoot, True ):
sNewDir = oReL.sub( r'\g<1>0\2', sDir )
if sNewDir != sSubDir: print '%s changed to %s' % (sDir, sNewDir)
else:
sNewDir = oReQ.sub( r'\g<1>0\2', sDir )
if sNewDir != sSubDir: print '%s changed to %s ' % (sDir, sNewDir)
print 'Root %s DONE.' % sRoot
Renaming the content of the current directory:
# Iterate roots
for sRoot in aRoots:
print '\nWalking root %s...\n' % sRoot
# Walk-down root
for sDir, aDirs, aFiles in os.walk( sRoot, True ):
for sSubDir in aDirs:
sNewSubDir = oReL.sub( r'\g<1>0\2', sSubDir )
if sNewSubDir != sSubDir: print '%s changed to %s' % (sSubDir, sNewSubDir)
else:
sNewSubDir = oReQ.sub( r'\g<1>0\2', sSubDir )
if sNewSubDir != sSubDir: print '%s changed to %s ' % (sSubDir, sNewSubDir)
print 'Root %s DONE.' % sRoot
Thanks
Bernard
More information about the Tutor
mailing list