3

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
chaNcharge
  • 239
  • 1
  • 2
  • 12
  • Why not use split on a string to separate phrases inbetween “/“ and “.” characters. –  Mar 11 '18 at 07:47
  • Possible duplicate of [How to split a dos path into its components in Python](https://stackoverflow.com/questions/3167154/how-to-split-a-dos-path-into-its-components-in-python) – three_pineapples Mar 11 '18 at 10:37
  • I know how to split a path in regular python, but my PyQt program turns a path into a QUrl. Would os.path.split work the same way on a QUrl? Sorry, I'm still learning PyQt. – chaNcharge Mar 11 '18 at 18:33

3 Answers3

7

There are several simple ways to get the name of a file:

  • Using QUrl:

song, _ = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")
print(song)
url = QUrl.fromLocalFile(song)
self.playlist.addMedia(QMediaContent(url))
your_statusbar.showMessage("now playing {}".format(url.fileName()))
  • Using QFileInfo:

song, _ = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")
print(song)
url = QUrl.fromLocalFile(song)
self.playlist.addMedia(QMediaContent(url))
filename = QFileInfo(song).fileName()
your_statusbar.showMessage("now playing {}".format(filename))
  • Using pathlib:

song, _ = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")
print(song)
url = QUrl.fromLocalFile(song)
self.playlist.addMedia(QMediaContent(url))

from pathlib import Path    

filename = Path(song).name
your_statusbar.showMessage("now playing {}".format(filename))
  • Using os:

song, _ = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")
print(song)
url = QUrl.fromLocalFile(song)
self.playlist.addMedia(QMediaContent(url))

import os   

filename = song.rstrip(os.sep)
your_statusbar.showMessage("now playing {}".format(filename))

or:

song, _ = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")
print(song)
url = QUrl.fromLocalFile(song)
self.playlist.addMedia(QMediaContent(url))

import os   

_ , filename = os.path.split(os.sep)
your_statusbar.showMessage("now playing {}".format(filename))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
2

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.

filepath = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")[0]
filename = filepath.split("/")[-1]

print(filename)

After that you can simply use

self.<statusbarname>.showMessage("Now playing {0} song or whatever".format(filename))

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.

import os # Careful with this library, Read the documentation first
filepath = os.path.normpath(filepath) # Normalize it
filename = filepath.split(os.sep) # Slice it

The entire code should work like this:

import os
filepath = QFileDialog.getOpenFileName(self, "Open Song", "~", "Sound Files (*.mp3 *.ogg *.wav *.m4a)")[0]
print(filepath)
filepath = os.path.normpath(filepath)
song = filepath.split(os.sep)
url = QUrl.fromLocalFile(filepath)
self.playlist.addMedia(QMediaContent(url))
self.<statusbarname>.showMessage("Now playing {0} song or whatever and it was at {1} folder".format(song, filepath))
Saelyth
  • 1,694
  • 2
  • 25
  • 42
0

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.