using functions and file renaming problem

hokiegal99 hokiegal99 at hotmail.com
Sat Jul 19 20:09:49 EDT 2003


Well, I think I'm finished. Thanks to everyone for the advice and in 
depth explanations. Below is the entire fix_mac_names.py script (some of 
it taken directly from you guys). I think my recursion is a bit of a 
kludge as is the format of my output, but other than that I think it's 
OK. I'm open to suggestions if anyone sees anything blatantly wrong with it.

Thanks again!


print " "
import os, re, string
setpath = raw_input("Path to the directory where you would like to fix 
names: ")
print " "
print "--- Replace Bad Characters in Directory Names & Filenames ---"
print " "
def clean_names(setpath):
	bad = re.compile(r'%2f|%25|[*?<>/\|\\]') #search for these bad chars..
	for root, dirs, files in os.walk(setpath):
		for dir in dirs:
			badchars = bad.findall(dir)	# find any bad characters
          		newdir = dir
          		for badchar in badchars:	# loop through each character in 
badchars
          			print "replaced:	",badchar,"	in dir		",newdir,"		",
              			newdir = newdir.replace(badchar,'-') #replace bad chars.
				print newdir
			if newdir:	# If there were any bad characters in the name, do this:
              			newpath = os.path.join(root,newdir)
              			oldpath = os.path.join(root,dir)
              			os.rename(oldpath,newpath)
	for root, dirs, files in os.walk(setpath):
		for file in files:
			badchars = bad.findall(file)	# find all bad chars.
          		newfile = file
          		for badchar in badchars:	# loop through each character in 
badchars
          			print "replaced:	",badchar,"	in file		",newfile,"		",
              			newfile = newfile.replace(badchar,'-') #replace bad chars.
				print newfile
			if newfile:	# If there were any bad characters in the name, do this:
              			newpath = os.path.join(root,newfile)
              			oldpath = os.path.join(root,file)
              			os.rename(oldpath,newpath)
clean_names(setpath)	#1
clean_names(setpath)	#2
clean_names(setpath)	#3
clean_names(setpath)	#4
clean_names(setpath)	#5
clean_names(setpath)	#6
clean_names(setpath)	#7
clean_names(setpath)	#8
clean_names(setpath)	#9
clean_names(setpath)	#10
print " "
print "--- Done ---"
print " "
print "--- Remove Spaces from Beginning and Ending of Directory Names & 
Filenames ---"
print " "
def clean_spaces(setpath):
	for root, dirs, files in os.walk(setpath):
     		for dir in dirs:
         		old_dname = dir
         		new_dname = old_dname.strip()
			if new_dname != old_dname:
				print "removed spaces from dir:",old_dname
				newpath = os.path.join(root,new_dname)
        				oldpath = os.path.join(root,old_dname)
        				os.rename(oldpath,newpath)
	for root, dirs, files in os.walk(setpath):
     		for file in files:
         		old_fname = file
         		new_fname = old_fname.strip()
			if new_fname != old_fname:
				print "removed spaces from file:",old_fname
				newpath = os.path.join(root,new_fname)
        				oldpath = os.path.join(root,old_fname)
        				os.rename(oldpath,newpath)
clean_spaces(setpath)	#1
clean_spaces(setpath)	#2
clean_spaces(setpath)	#3
clean_spaces(setpath)	#4
clean_spaces(setpath)	#5
clean_spaces(setpath)	#6
clean_spaces(setpath)	#7
clean_spaces(setpath)	#8
clean_spaces(setpath)	#9
clean_spaces(setpath)	#10
print " "
print "--- Done --- "
print " "





More information about the Python-list mailing list