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!

Recommended Answers

All 6 Replies

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)

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

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.'

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

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)

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.'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.