0

I am trying to open a text file in notepad++ using subprocess.Popen

I have tried many variations of the following code, but none of them work:

 subprocess.Popen(['start notepad++.exe', 'C:\Python27\Django_Templates\QC\postits.html'])

 subprocess.Popen(['C:\Program Files (x86)\Notepad++\notepad++.exe', 'C:\Python27\Django_Templates\QC\postits.html'])

There must be a way to do this.

NOTE:

I want to use subprocess.Popen and not os.system because I want to poll to determine whether the program is still open and do something after it closes.

So, this is part of the following code block:

process = subprocess.Popen(.....)
while process.poll() is None:
    sleep(10)
DO SOMETHING AFTER FILE CLOSES

If Popen is not the best way to do this, I am open to any solution that allows me to poll my system (Windows) to determine whether notepad++ is still open, and allows me to do something after it closes.

0

1 Answer 1

3

I expect that the problem is that your path separators are backslashes which is the Python string escape character.

Use raw strings instead:

subprocess.Popen([
    r'C:\Program Files (x86)\Notepad++\notepad++.exe', 
    r'C:\Python27\Django_Templates\QC\postits.html'
])

Or escape the backslashes:

subprocess.Popen([
    'C:\\Program Files (x86)\\Notepad++\\notepad++.exe', 
    'C:\\Python27\\Django_Templates\\QC\\postits.html'
])

Or, even better, use os.path.join rather than hard-coded path separators.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey @david why doesn't this work: subprocess.Popen([r"C:\Python27\lib\idlelib\idle.py", r"C:\Python27\programs\test2.py"]) - where I am trying to do the same thing as above (only with Pythong IDE).
Because the first argument is not an executable, I guess. Surely you need to pass pythonw.exe as the first arg.

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.