Ethan Whitt
Ethan Whitt

Reputation: 167

Print __file__ without file extension

Is there a better way to print __file__ without the extension?

import os
print os.path.splitext(__file__)[0]

Upvotes: 4

Views: 2238

Answers (4)

Chinmay Kanchi
Chinmay Kanchi

Reputation: 66033

Don't do this, use os.path.splitext. However if you must, here's a way:

'.'.join(__file__.split('.')[:-1])

Upvotes: 4

Zach Kelling
Zach Kelling

Reputation: 53879

>>> 'my.cool.script.py'.rsplit('.', 1)[0]
<<< 'my.cool.script'

Upvotes: 3

maligree
maligree

Reputation: 6157

Without split(), without os: file[:file.find('.')].

(inb4 a captain obvious jumps in with a comment: with the assumption that you have one '.' in file).

Upvotes: 0

Adam Matan
Adam Matan

Reputation: 136451

You can use string.split, but what's the point? The standard library is giving you the exact tool you need.

Upvotes: 12

Related Questions