Quantcast
Channel: How do I get the filename without the extension from a path in Python? - Stack Overflow
Viewing all articles
Browse latest Browse all 66

Answer by amc for How to get the filename without the extension from a path in Python?

$
0
0

I think the easiest way is to use .split("/")

input= "PATH/TO/FILE/file.txt" file_only = input.split("/")[-1] print(file_only)

>>> file.txt

You can also do this to extract the last folder:

input= "PATH/TO/FOLDER" folder_only = input.split("/")[-1] print(folder_only)

>>> FOLDER

If you want the penultimate folder, simply change [-1] to [-2].


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>