我有一个 python 脚本,它创建一个 GUI 应用程序,在用户选择路径后显示现有文件的列表。
系统显示在QlistWidget文件名及其父文件夹。
我想从列表中获取所选文件的完整路径
例子 :C:\Users\test\Desktop\test_arabic.docx
系统显示此错误:
[WinError 267] 目录名称无效:
'C:\Users\test\Desktop\test_arabic.docx'
如何修复此错误。
注意:我使用 self.fullpath 变量作为全局变量。
print("chdir ==> {}".format(os.chdir(PureWindowsPath(self.fullPath))))
上面的代码就是导致错误的原因。我试图将当前目录路径更改为文件的选定路径。
def eventFilter(self, obj, event):
try:
if self.listWidgetPDFlist.viewport() == obj and event.type() == QtCore.QEvent.MouseMove:
#*********
it = self.listWidgetPDFlist.itemAt(event.pos())
print("it ==>{}".format(it.text()))
##*********
Item=str(it.text())
print("item ==> {}".format(Item))
print("full path ==>{}".format(PureWindowsPath(self.fullPath)))
print("chdir ==> {}".format(os.chdir(PureWindowsPath(self.fullPath))))
parentFile =Path().resolve().parent
print("parent file ==>{}".format(parentFile))
selectedFile = os.path.join(str(parentFile,Item))
print("selectedFile ==>{}".format(selectedFile))
if self.hoverItem != it:
message = selectedFile
QtWidgets.QToolTip.showText(QtGui.QCursor.pos(),
message,
self.listWidgetPDFlist.viewport(),
self.listWidgetPDFlist.visualItemRect(it))
self.hoverItem = selectedFile
except Exception as e:
print(e)
return QtWidgets.QMainWindow.eventFilter(self, obj, event)