I'm working on a certain program and I need to have it do different things if the file in question is a flac file, or an mp3 file. Could I just use this?
if m == *.mp3 .... elif m == *.flac ....
I'm not sure whether it will work.
EDIT: When I use that, it tells me invalid syntax. So what do I do?
Look at module fnmatch. That will do what you're trying to do.
Use
pathlib
From Python3.4 onwards.os.path
provides many functions for manipulating paths/filenames. (docs)os.path.splitext
takes a path and splits the file extension from the end of it.Gives:
one easy way could be:
os.path.splitext(file)
will return a tuple with two values (the filename without extension + just the extension). The second index ([1]) will therefor give you just the extension. The cool thing is, that this way you can also access the filename pretty easily, if needed!