Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 16th, 2004, 08:24 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 95 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 2 m 36 sec
Reputation Power: 5
Listing file names w/o the extensions

Greets,

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?

*edit*
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

Code:
list=os.listdir('path/to/dir/')
            for i in list:
                print i[:-4]


This works just fine for me,and allowes me to display the file name w/o the extension
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

Reply With Quote
  #2  
Old November 18th, 2004, 05:25 AM
Konstandinos Konstandinos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: Cape Town, South Africa
Posts: 131 Konstandinos User rank is Corporal (100 - 500 Reputation Level)Konstandinos User rank is Corporal (100 - 500 Reputation Level)Konstandinos User rank is Corporal (100 - 500 Reputation Level)Konstandinos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 40 m 25 sec
Reputation Power: 7
Lightbulb Ignoring file extensions on dir.list

Hi there.

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.

Cheers,
K

Reply With Quote
  #3  
Old November 18th, 2004, 06:58 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
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:
Code:
>>> import glob
>>> textfiles = [x[:-4] for x in glob.glob("*.txt")]
>>> textfiles
['MOTD', 'LICENSE', 'README', 'NEWS', 'text', 'log1', 'log2', 'diff', 'Document1', 'Document2']


grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #4  
Old November 18th, 2004, 10:06 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
What if some of the files have longer extensions?

Code:
>>> import os, sys
>>> os.path.extsep
'.'
>>> os.path.splitext("c:\\a.txt")
('c:\\a', '.txt')

>>> import glob
>>> f = glob.glob("e:\\*.txt")
>>> f[0]
'e:\\contacts.txt'

>>> os.path.basename(f[0])
'contacts.txt'

>>> [os.path.splitext(os.path.basename(x))[0] for x in glob("d:\\*.txt")



Reply With Quote
  #5  
Old November 18th, 2004, 11:03 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Yep, that was a more robust solution


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Listing file names w/o the extentions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT