making a valid file name...

SpreadTooThin bjobrien62 at
Tue Oct 17 18:22:45 CEST 2006

Hi I'm writing a python script that creates directories from user
Sometimes the user inputs characters that aren't valid characters for a
file or directory name.
Here are the characters that I consider to be valid characters...

valid =
':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '

if I have a string called fname I want to go through each character in
the filename and if it is not a valid character, then I want to replace
it with a space.

This is what I have:

def fixfilename(fname):
	valid =
':.\,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
	for i in range(len(fname)):
		if valid.find(fname[i]) < 0:
			fname[i] = ' '
       return fname

Anyone think of a simpler solution?

More information about the Python-list mailing list