Is there another better way to get file ext from a given path in python -
i'm new in python, know, can file ext path use:
os.path.splitext(path)[1][1:]
this works well, seems not quite beautiful, want ask: there better way file ext
the readable way be
name, ext = os.path.splitext(path) ext = ext[1:]
though that's no longer single expression. if want single expression, wrap in function:
def extension(path): name, ext = os.path.splitext(path) return ext[1:]
be aware on systems, files may have empty extension, e.g.
>>> os.path.splitext('ham.') ('ham', '.')
and you're treating same files no extension @ (just ham
). usually, difference doesn't matter, in cases might, why splitext
works way does.
Comments
Post a Comment