Vischi
Vischi

Reputation: 43

How could I get the name of a specific file but not use the extension from a path?

I found out a method called os.path.basename to get the filename with extension. But even when I import os, I am not able to call it path.basename. Is it possible to call it as directly as basename?

Upvotes: 0

Views: 42

Answers (2)

vash_the_stampede
vash_the_stampede

Reputation: 4606

Having a little trouble understanding your problem at hand, just throwing some ideas out there

1

>>> import os 
>>> name = os.path.basename('~/python/data.txt')
>>> name
'data.txt'

2

>>> from os import path
>>> name = path.basename('~/python/data.txt')
>>> name
'data.txt'

3

>>> from os.path import basename
>>> name = basename('~/python/data.txt')
>>> name
'data.txt'

Upvotes: 1

Andy Chang
Andy Chang

Reputation: 79

Is it you are looking for?

from os.path import basename
basename('~/test.txt')

Upvotes: 2

Related Questions