I run the program with Python, but there is a space in the middle of the path, so I get an error

Asked 2 years ago, Updated 2 years ago, 18 views

The program runs on Python, but there is a space in the middle of the path, so an error appears

import os;
os.system("C:\\Temp\\a b c\\Notepad.exe"); #Notepad.exe is an exe that I made myself
raw_input();

'C:\Temp\a' is not recognized as an internal or external command, operable program or batch file.

It will run if you wrap it in a different quotation mark, but this will cause an error when writing the parameter.

import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe" "C:\\test.txt"');
raw_input();

If I change the path name, I have to change a lot of other related things, so I'm going to leave the path if possible. How can I hand over the parameters while maintaining the path?

python shellexecute

2022-09-22 22:15

1 Answers

The issue should be subprocess.call instead of os.system.

You don't have to pay much attention to the space because you get the list as a factor, not a string, and you use a comma (,) to distinguish the factor

import subprocess
subprocess.call(['C:\\Temp\\a b c\\Notepad.exe', 'C:\\test.txt'])


2022-09-22 22:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.