Don't open the files for writing, it will erase the files content !!! In fact you don't need to open the files at all. I suggest that you first create a dictionary newname --> oldname, this allows to detect the potential name collisions before renaming anything. Here is a possible script
import os
import re
startdigits = re.compile(r"^\d+")
def create_map():
result = dict()
for filename in os.listdir("."):
if not os.path.isfile(filename): # skip subdirectories
continue
newfilename = startdigits.sub("", filename)
if newfilename in result or (newfilename != filename
and os.path.isfile(newfilename)):
raise Exception("Name collision detected for '%s'" % filename)
if newfilename != filename:
result[newfilename] = filename
if __name__ == "__main__":
filemap = create_map()
for new, old in filemap.items():
os.rename(old, new)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
You don't save the original file name so there is no "copy from" name. Be sure to back up the directory before testing any rename code.
import os
#read in file from the directory
for filename in os.listdir("."):
# f = open(filename, "w")
# i = True
# while i: doesn't do anything
#if filename starts with a digit, lose first index by renaming and
#try again
new_filename = filename
while new_filename[0].isdigit():
new_filename = new_filename[1:]
if new_filename != filename:
print "renaming %s to %s" % (filename, new_filename)
os.rename(filename, new_filename)
print 'Any filename starting with a digit has been renamed.'
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
Thanks for the help...but when I try running that code I get a SyntaxError: invalid syntax at line 11...
Sorry, parenthise line 11 like this
if ((newfilename in result) or (newfilename != filename
and os.path.isfile(newfilename))):
raise Exception("Name collision detected for '%s'" % filename)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
You don't save the original file name so there is no "copy from" name. Be sure to back up the directory before testing any rename code.
import os
#read in file from the directory
for filename in os.listdir("."):
# f = open(filename, "w")
# i = True
# while i: doesn't do anything
#if filename starts with a digit, lose first index by renaming and
#try again
new_filename = filename
while new_filename[0].isdigit():
new_filename = new_filename[1:]
if new_filename != filename:
print "renaming %s to %s" % (filename, new_filename)
os.rename(filename, new_filename)
print 'Any filename starting with a digit has been renamed.'
I would use built in lstrip():
import os
import string
for filename in os.listdir(os.curdir):
new_filename = filename.lstrip(string.digits)
if new_filename != filename:
if os.path.isdir(filename):
print "Dir name not changed", filename, '->', new_filename
elif os.path.isfile(new_filename):
print "File name in use, not renamed", filename, '->', new_filename
else:
print "Renaming %s to %s" % (filename, new_filename)
os.rename(filename, new_filename)
print 'Any filename starting with a digit has been renamed.'
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852