PyQt5 - Get file name from an opened file, not a file path

こ雲淡風輕ζ 提交于 2019-12-11 02:30:42

问题


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?


回答1:


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))



回答2:


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.




回答3:


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))


来源:https://stackoverflow.com/questions/49217347/pyqt5-get-file-name-from-an-opened-file-not-a-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!