无法在exp中打开文件

2024-10-03 21:32:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经检查了here,并且独立地尝试了代码,它工作了…但是在我的QTreeView应用程序中插入代码…它打开了以“我的文档”为焦点的浏览器用户库

我使用subprocess.Popen(r'explorer /select,"file_path"')

这是我的密码

def on_clicked(self, index):
    # self.path = self.fileSystemModel.fileInfo(index).absoluteFilePath()
    self.path = self.fileSystemModel.filePath(index)
    print(self.path

def tabMenu(self, positon):
    self.tmenu = QMenu()

    self.open = self.tmenu.addAction('Open')
    self.open_file_location = self.tmenu.addAction('Open File Location')

    self.tmenu.addActions([self.open, self.open_file_location])
    action = self.tmenu.exec_(self.temp_treeView.viewport().mapToGlobal(position))

    if action == self.open:
        os.startfile(self.path, 'open')
    elif action == self.open_file_location:
        print(self.path)
        subprocess.Popen(r'explorer /select,' + self.path)

Tags: path代码selfindexdefactionlocationopen
1条回答
网友
1楼 · 发布于 2024-10-03 21:32:56

self.path = self.fileSystemModel.filePath(index)返回带有/的文件路径,subprocess.Popopen(r'explorer /select,"path")无法访问该路径。.您需要将/转换为\才能正常工作…通过subprocess.Popen(r'explorer /select,'+"{}".format(path).replace('/', '\\'))转换字符串

相关问题 更多 >