Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - How to get the extension of a filename from the path

 
Thread Tools Search this Thread
Old 12-08-2005, 11:34 AM   #1
Default How to get the extension of a filename from the path


Hello,
what is a way to get the the extension of a filename from the path?
E.g., on my XP windows the path can be
C:\Pictures\MyDocs\test.txt
and I would like to get
the the extension of the filename, that is here
txt


I would like that to work on Linux also
Thank you for help
L.



Lad
  Reply With Quote
Old 12-08-2005, 11:44 AM   #2
Dale Strickland-Clark
 
Posts: n/a
Default Re: How to get the extension of a filename from the path
Lad wrote:

> Hello,
> what is a way to get the the extension of a filename from the path?
> E.g., on my XP windows the path can be
> C:\Pictures\MyDocs\test.txt
> and I would like to get
> the the extension of the filename, that is here
> txt
>
>
> I would like that to work on Linux also
> Thank you for help
> L.


Like this, you mean?
>>> import os.path
>>> os.path.splitext("c:\\pictures\\mydocs\\test.txt")

('c:\\pictures\\mydocs\\test', '.txt')

--
Dale Strickland-Clark
Riverhall Systems www.riverhall.co.uk




Dale Strickland-Clark
  Reply With Quote
Old 12-08-2005, 11:46 AM   #3
Tom Anderson
 
Posts: n/a
Default Re: How to get the extension of a filename from the path
On Thu, 8 Dec 2005, Lad wrote:

> what is a way to get the the extension of a filename from the path?
> E.g., on my XP windows the path can be
> C:\Pictures\MyDocs\test.txt
> and I would like to get
> the the extension of the filename, that is here
> txt


You want os.path.splitext:

>>> import os
>>> os.path.splitext("C:\Pictures\MyDocs\test.txt")

('C:\\Pictures\\MyDocs\test', '.txt')
>>> os.path.splitext("C:\Pictures\MyDocs\test.txt")[1]

'.txt'
>>>


> I would like that to work on Linux also


It'll be fine.

tom

--
[Philosophy] is kind of like being driven behind the sofa by Dr Who -
scary, but still entertaining. -- Itchyfidget


Tom Anderson
  Reply With Quote
Old 12-08-2005, 11:49 AM   #4
Fredrik Lundh
 
Posts: n/a
Default Re: How to get the extension of a filename from the path
"Lad" <> wrote:

> what is a way to get the the extension of a filename from the path?
> E.g., on my XP windows the path can be
> C:\Pictures\MyDocs\test.txt
> and I would like to get
> the the extension of the filename, that is here
> txt
>
> I would like that to work on Linux also
> Thank you for help


os.path.splitext(filename) splits a filename into a name part (which may include
a path) and an extension part:

import os
f, e = os.path.splitext(filename)

the extension will include the separator, so the following is always true:

assert f + e == filename

if you don't want the period, you can strip it off:

if e[:1] == ".":
e = e[1:]

but it's often easier to change your code to take the dot into account; instead
of

if e[:1] == ".":
e = e[1:]
if e == "txt":
handle_text_file(filename)
elif e in ("png", "jpg"):
handle_image_file(filename)

do

if e == ".txt":
handle_text_file(filename)
elif e in (".png", ".jpg"):
handle_image_file(filename)

on the other hand, for maximum portability, you can use

f, e = os.path.splitext(filename)
if e.startswith(os.extsep):
e = e[len(os.extsep):]
if e == "txt":
...

but that's probably overkill...

</F>





Fredrik Lundh
  Reply With Quote
Old 12-08-2005, 12:26 PM   #5
gene tani
 
Posts: n/a
Default Re: How to get the extension of a filename from the path

Lad wrote:
> Hello,
> what is a way to get the the extension of a filename from the path?
> E.g., on my XP windows the path can be
> C:\Pictures\MyDocs\test.txt
> and I would like to get
> the the extension of the filename, that is here
> txt
>
>
> I would like that to work on Linux also
> Thank you for help
> L.


minor footnote: windows paths can be raw strings for os.path.split(),
or you can escape "/"
tho Tom's examp indicates unescaped, non-raw string works with
splitext()

import os.path
# winpath='C:\\Pictures\\MyDocs\\test.txt'
winpath=r'C:\Pictures\MyDocs\test.txt'
fpath,fname_ext=os.path.split(winpath)
print "path: %s ;;;;; fname and ext: %s"%(fpath, fname_ext)
ext=fname_ext.split(".")[-1]
print ext



gene tani
  Reply With Quote
Old 12-08-2005, 02:15 PM   #6
Peter Hansen
 
Posts: n/a
Default Re: How to get the extension of a filename from the path
Fredrik Lundh wrote:
> on the other hand, for maximum portability, you can use
>
> f, e = os.path.splitext(filename)
> if e.startswith(os.extsep):
> e = e[len(os.extsep):]
> if e == "txt":
> ...


Is there ever a time when the original `e` could evaluate True, yet not
startswith(os.extsep)? In other words, could the first test be just

if e:
e = e[len(os.extsep):]

Also, note that for truly maximum portability one probably needs to add
to the code some knowledge of case-sensitivity and do a .lower() when
appropriate, as "txt" and "TXT" (and others) are equivalent on Windows
file systems. On that note, is there a common idiom for detecting that
information?

-Peter



Peter Hansen
  Reply With Quote
Old 12-08-2005, 03:06 PM   #7
Lad
 
Posts: n/a
Default Re: How to get the extension of a filename from the path
Thank you ALL for help
Regards,
L.



Lad
  Reply With Quote
Old 12-08-2005, 04:15 PM   #8
gene tani
 
Posts: n/a
Default Re: How to get the extension of a filename from the path

Lad wrote:
> Thank you ALL for help
> Regards,
> L.


addendum: ASPN Python cookbook often has something relevant /
modifiable for your needs:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81931
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52661

(in this case code from 2001 / 2 is probably py 2.0 or 2.1, shd still
work)



gene tani
  Reply With Quote
Old 12-09-2005, 10:45 AM   #9
Tom Anderson
 
Posts: n/a
Default Re: How to get the extension of a filename from the path
On Thu, 8 Dec 2005, gene tani wrote:

> Lad wrote:
>
>> what is a way to get the the extension of a filename from the path?

>
> minor footnote: windows paths can be raw strings for os.path.split(),
> or you can escape "/"
> tho Tom's examp indicates unescaped, non-raw string works with
> splitext()


DOH. Yes, my path's got a tab in it, hasn't it!

tom

--
Women are monsters, men are clueless, everyone fights and no-one ever
wins. -- cleanskies


Tom Anderson
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript relative path to load xml file torontodesi Software 2 02-26-2009 01:13 PM
"src" path changing automatically?? lisaj Software 0 04-03-2008 11:36 AM
Orcas Implications on Current MCTS Web Developer Path John2021 MCTS 11 11-22-2007 03:02 PM
how to upload a folder path??? chinna Software 0 10-03-2006 11:47 AM
Set default path of <input type = file> vj_india Software 1 09-15-2006 06:17 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46