PDA

View Full Version : Help removing file extensions from filename list



jondecker76
November 9th, 2007, 03:56 AM
Hello

I have a list of filenames filled by os.listdir()

How can I remove the file extensions from this list?
I was looking at os.path.splitext() but it doesn't work on lists, and it returns a list of both the filename and the extension...

Any hints?

thanks,

Jon

jondecker76
November 9th, 2007, 04:14 AM
Hello again,

I have figured out a way to do it, but I feel as if I am way overcomplicating it (though it works).. Here is the ugly version I came up with.. Can someone critique this code and show me a better way to do it?



#Get a lits of file names
filenames = os.listdir('/home/jondecker76/ROMS/TG16/')
#Now remove the file extensions
i = 0

for thisItem in filenames:
filenames[i] = os.path.splitext(thisItem)[0]
i=i+1

#Lets see how they look
print filenames

cwaldbieser
November 9th, 2007, 06:40 AM
files = os.path.listdirs('.')
files = [os.path.splitext(x)[0] for x in files]

jondecker76
November 9th, 2007, 10:37 AM
I knew there had to be a more elegant way - thank you!