954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Removing Numbers From the Beginning of Filenames

I have a large number of files that begin with numbers (e.g., 10admin_boundary_x) and would like to rename the files so that they do not begin with a digit (e.g., admin_boundary_x). I am working with shapefiles (.shp, .shx, .dbf, etc) and thought a python script could save me some time.

I haven't quite figured it out yet, but here is the code I've got so far:

import os

#read in file from the directory
for filename in os.listdir("."):
    
    f = open(filename, "w")
    
    i = True
	
    while i:
	#if filename starts with a digit, lose first index by renaming and 
        #try again
	while filename[0].isdigit():
	    
	    filename = filename[1:]
            
        os.rename(f, filename)
        
        i = False    
	        
print 'Any filename starting with a digit has been renamed.'


Thank you for your help!

csterling
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Thanks for the help...but when I try running that code I get a SyntaxError: invalid syntax at line 11...

csterling
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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
 

Thank you so much woooee! That helped immensely! :D

csterling
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
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
Moderator
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
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: