PyQt5 - 从打开的文件中获取文件名,而不是文件路径

时间:2018-03-11 06:33:35

标签: python pyqt pyqt5 qtmultimedia

假设我使用文件对话框在PyQt5应用程序中打开了一个名为file1.mp3的文件,并将其分配给这样的变量:

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

如何获取文件名而不是文件路径,以便在statusBar中显示?或者更好的是,我可以使用或创建一个“正在播放”的功能吗?

3 个答案:

答案 0 :(得分:3)

有几种简单的方法可以获取文件名称:

  • 使用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()))
  • 使用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))
  • 使用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))
  • 使用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))

或:

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

答案 1 :(得分:0)

编程不是魔术,你有一个文件路径,即: c://myfolder/song.mp3 - 假设您的音乐文件以歌曲命名,您必须解析歌曲名称的网址,并将状态栏标题/标签设置为您当前正在播放的歌曲。我建议你在将qt框架混合到一起之前在python上进行一个入门lvl课程。

答案 2 :(得分:0)

Self-explanatory。你只需要切割字符串。而且因为你正在学习,我会以错误的方式切片,为你找出原因。

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

print(filename)

之后你可以简单地使用

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

但是,这只适用于“某些”系统。如果要在另一台计算机上使用该应用程序,则应首先规范路径(某些系统使用//而其他系统使用文件夹),然后使用安全的内置命令对其进行切片。

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

整个代码应该像这样工作:

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