[Tutor] Partly OT: order of renaming files and directories

Kent Johnson kent37 at tds.net
Mon Sep 12 16:49:20 CEST 2005


Bernard Lebel wrote:
> 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?

Assuming you are going to actually rename the directories on disk (the code below just changes the names in a local string) I don't think the second approach will work as written. The list of directories yielded by os.walk() can be changed in the client to affect which sub-directories are iterated. From the docs on os.walk():

"...the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again."

So the list of dirnames is the list that will be attempted in the recursive step; if you rename them without updating dirnames the renamed subdir will not be found. At least that is how I read it; you could easily test this by trying it and looking at the log.

So I would do the first method. If you want to use the second approach I think you will have to modify dirnames as well as renaming the file on disk.

Kent
> 
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 



More information about the Tutor mailing list