Reputation: 167
Is there a better way to print __file__
without the extension?
import os
print os.path.splitext(__file__)[0]
Upvotes: 4
Views: 2238
Reputation: 66033
Don't do this, use os.path.splitext
. However if you must, here's a way:
'.'.join(__file__.split('.')[:-1])
Upvotes: 4
Reputation: 53879
>>> 'my.cool.script.py'.rsplit('.', 1)[0]
<<< 'my.cool.script'
Upvotes: 3
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
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