0

i have checked here, and tried the code independently and it worked...but inserting the code in my QTreeView app...it opens up the explorer user libraries with 'My Documents' in focus

i use subprocess.Popen(r'explorer /select,"file_path"')

here is my code

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
X-Black...
  • 1,376
  • 2
  • 20
  • 28
  • @eyllanesc no error from cmd – X-Black... May 28 '18 at 11:17
  • @eyllanesc Tried it...no changes – X-Black... May 28 '18 at 11:27
  • @eyllanesc yes..i added a print() to be sure...it's a full path – X-Black... May 28 '18 at 11:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171911/discussion-between-x-black-and-eyllanesc). – X-Black... May 28 '18 at 11:41
  • @eyllanesc output = `explorer /select,"C:/Users/Black Laptop/Videos/two mad boys.mp4"` – X-Black... May 28 '18 at 11:44
  • @eyllanesc found something... running the process alone gave the output = explorer /select,"C:\Users\Black Laptop\Videos\tom and jerry.mp4" while in the TreeView app self.path returns `explorer /select,"C:/Users/Black Laptop/Videos/two mad boys.mp4"` ...different slashes...is there anyway i can convert the `/`..? – X-Black... May 28 '18 at 12:14
  • @eyllanesc yes...the `explorer /select,"C:\Users\Black Laptop\Videos\tom and jerry.mp4"` worked but `explorer /select,"C:/Users/Black Laptop/Videos/two mad boys.mp4"` don't – X-Black... May 28 '18 at 12:22
  • @eyllanesc using `subprocess.Popen(r'explorer /select,"{}"'.format(path).replace("/", "\\"))` opens the file...so i used `subprocess.Popen(r'explorer /select,'+"{}".format(path).replace('/', '\\'))` ...and got it to work...thanks – X-Black... May 28 '18 at 12:32

1 Answers1

0

self.path = self.fileSystemModel.filePath(index) returns file path with / which cannot be accessed by the subprocess.Popopen(r'explorer /select,"path")..you need to convert the/ to \ to make it work...converted the string by subprocess.Popen(r'explorer /select,'+"{}".format(path).replace('/', '\\'))

X-Black...
  • 1,376
  • 2
  • 20
  • 28