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 Connor for How to get the filename without the extension from a path in Python?

$
0
0

For maximum esoterica, and for a fun oneliner, and to learn a little about itertools:

def strip_suffix(filename):
    """
    >>> video.mp4
    video

    >>> video.extra.mp4
    video.extra
    """
    return ''.join((name_dot[0] + name_dot[1] for name_dot in itertools.zip_longest(filename.split('.')[0:-1], '.', fillvalue='.')))[0:-1]

Note: this is just for fun. Do not use this. Use os.path.splitext instead


Viewing all articles
Browse latest Browse all 66

Trending Articles



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