November 16th, 2004, 07:24 PM
Listing file names w/o the extensions
I have a small program I use to keep track of some of my customers info.All customer info is written into a text file(each file named after the customer),and I have a option to list the files in the customers directory.I am currently using os.listdir() to display the files,but all the ".txt" extensions makes reading a long list a little hard on the eye.I could slice the extensions off,but I suspect there is a better way to do it.
Can anyone recommend a way to list the files without showing the extensions?
Sorry for this,but since I can't delete the thread,I guess I'll just post what I have done using slices,and fix spelling errors
This works just fine for me,and allowes me to display the file name w/o the extension
for i in list:
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
November 18th, 2004, 04:25 AM
Ignoring file extensions on dir.list
I had a similar problem in the past, and solved it using the exact same method as you.
I believe that the functionality/method you're looking for doesn't actually exist.
Why not send the guys over at python.org an e-mail requesting an extension of the glob module?
The glob.glob('*') which is similar/identical to your os.listdir() method could be extended to return a list without file extensions by calling it with glob.glob('*', NO_EXTENSION) for example.
Just a thought.
November 18th, 2004, 05:58 AM
Just a thought - what if the directory does not just contain files with the right extension?
I think it would be safer to try this:
>>> import glob
>>> textfiles = [x[:-4] for x in glob.glob("*.txt")]
['MOTD', 'LICENSE', 'README', 'NEWS', 'text', 'log1', 'log2', 'diff', 'Document1', 'Document2']
November 18th, 2004, 09:06 AM
What if some of the files have longer extensions?
>>> import os, sys
>>> import glob
>>> f = glob.glob("e:\\*.txt")
>>> [os.path.splitext(os.path.basename(x)) for x in glob("d:\\*.txt")
November 18th, 2004, 10:03 AM
Yep, that was a more robust solution