devoured elysium
devoured elysium

Reputation: 105077

Is there a function that gives me a file name without path?

I want to turn C:\abc.bmp into abc.bmp, or even better, if possible, in abc. That is easy to do with .NET as there are functions for both goals. Is there anything similar in python?

Upvotes: 1

Views: 266

Answers (2)

unwind
unwind

Reputation: 399833

Try os.path.basename().

Upvotes: 3

SilentGhost
SilentGhost

Reputation: 319601

>>> os.path.basename(r'C:\abc.txt')
'abc.txt'

for basename only:

>>> base, ext = os.path.splitext(os.path.basename(r'C:\abc.txt'))
>>> base
'abc'

Upvotes: 11

Related Questions