0
import subprocess
path = r"C:/Users/Χρήστος/Desktop/Papinhio player old/notes/cases.txt"
subprocess.Popen(r'explorer /select,"'+path+'"')

When i run the up code i supposed to open the folder C:/Users/Χρήστος/Desktop/Papinhio player old/notes/ and highlight-select the file cases.txt

The folder and the file exists (for example: cd C:/Users/Χρήστος/Desktop/Papinhio player old/notes/ works.

I think the above code worked before some months, i don't know now why doesn't work. I have tried to run the code with two python versions: 3.9.2 and 3.8.9

Can you help me please about?

Instead of this, it opens the My Computer Explorer window. enter image description here

I found the problem:

import subprocess
import os

path = "C:/Users/Χρήστος/Desktop/Papinhio player old/notes/cases.txt"
path = os.path.abspath(path)
path = path.replace('/', '\\')
subprocess.Popen(r'explorer /select,"'+path+'"', shell=True)

But if there is a nicer answer i would like to know.

1
  • Just tested, and this seems to happen when the path of the file doesn't exist. Perhaps you've renamed your folders? Otherwise it could be related to some encoding/decoding of special greek characters Commented May 4, 2021 at 14:02

2 Answers 2

0

Run it with shell=True

import subprocess
path = r"C:/Users/Χρήστος/Desktop/Papinhio player old/notes/cases.txt"
subprocess.Popen(r'explorer /select,"'+path+'"', shell=True)
Sign up to request clarification or add additional context in comments.

4 Comments

No, i found it. if i replace /--> \\ then it works. Is there any function in path module that can i use?
yes, import os, os.path.join() is the correct way to parse file paths, you can also use the pathlib module
check the question. I have posted one answer. It is ok?
Yes it should also work using abspath, you won't need the replace though, the os.path module takes care of it
0
import subprocess
import os

path = "C:/Users/Χρήστος/Desktop/Papinhio player old/notes/cases.txt"
path = os.path.abspath(path)
path = path.replace('/', '\\')
subprocess.Popen(r'explorer /select,"'+path+'"', shell=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.