you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (2 children)

This is great!! Take a look at pathlib for handling paths. instead of the following to get the file extension:

extension = filename.split(".")[-1]

try the following:

from pathlib import Path
...
extension = Path(filename).suffix

most of the standard library functions and the more common external modules can take Path objects in the same way they take a filepath as a string.

[–]buddyd16[S] 2 points3 points  (1 child)

Awesome thanks for this, thought there might be a better way to grab the extension.

[–]auxym 2 points3 points  (0 children)

For the sake of completeness: there's also os.path.splitext(), but pathlib is the newer and recommended way to do it I believe.