By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

How to get the extension of a filename from the path

P: n/a
Lad
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.

Dec 8 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Dec 8 '05 #2

P: n/a
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
Dec 8 '05 #3

P: n/a
"Lad" <py****@hope.cz> 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>

Dec 8 '05 #4

P: n/a

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

Dec 8 '05 #5

P: n/a
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

Dec 8 '05 #6

P: n/a
Lad
Thank you ALL for help
Regards,
L.

Dec 8 '05 #7

P: n/a

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)

Dec 8 '05 #8

P: n/a
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
Dec 9 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.