Let's say I opened a file called file1.mp3 in a PyQt5 app using the file dialog and assigned it to a variable like this:
song = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")
print(song[0])
url = QUrl.fromLocalFile(song[0])
self.playlist.addMedia(QMediaContent(url))
How can I get the file name instead of a file path so I can display it in a statusBar? Or even better, is there a "now playing"-like function I could use or create?
There are several simple ways to get the name of a file:
QUrl
:QFileInfo
:pathlib
:os
:or:
programming is not magic, you have a file path i.e: c://myfolder/song.mp3 - assuming your music files are named after the song, you must parse the url for the song name and set the status bar title/label to the song you are currently playing. I suggest you take a entry lvl course on python before mixing qt frameworks into it.
Self-explanatory. You just need to slice the string. And because you are learning, I'll slice it the wrong way, for you to find out why.
After that you can simply use
However, that will only work on "some" systems. If you want to use that application on a different computer, you should first normalize the path (some systems use // and others \ for folders) and then you slice it with a safe built-in command.
The entire code should work like this: