Python3执行终端的方式与手动输入不同

时间:2021-08-18 17:29:02

I have a small piece of python3 code. Which runs a command from the terminal.

我有一小段python3代码。哪个从终端运行命令。

import os
os.system('"C:/directory/program.exe" -k "C:/directory/options.txt" & pause')

When I run this code in IDLE, I get the following error:

当我在IDLE中运行此代码时,我收到以下错误:

The filename, directory name, or volume label syntax is incorrect.

Both of the paths are valid. So thats not the problem. In addition, running:

两条路径都有效。所以那不是问题。另外,运行:

"C:/directory/program.exe" -k "C:/directory/options.txt" & pause

from the terminal works correctly.

从终端正常工作。

1 个解决方案

#1


0  

You don't need quotes around the system paths, this should work:

您不需要系统路径周围的引号,这应该工作:

import os
os.system("C:/directory/program.exe -k C:/directory/options.txt & pause")

Hopefully that helps.

希望这会有所帮助。

[Edit] Working with spaces like you're doing it with os.system is to my knowledge impossible, referring to this python bug tracker thread

[编辑]使用os.system就像你在使用os.system这样的空间是我的知识不可能,指的是这个python bug跟踪器线程

A solution might be using the subprocess module insead.

解决方案可能是使用子进程模块insead。

import subprocess
subprocess.call(["C:/direc tory/program.exe", "-k", "C:/direc tory/program.exe"])

#1


0  

You don't need quotes around the system paths, this should work:

您不需要系统路径周围的引号,这应该工作:

import os
os.system("C:/directory/program.exe -k C:/directory/options.txt & pause")

Hopefully that helps.

希望这会有所帮助。

[Edit] Working with spaces like you're doing it with os.system is to my knowledge impossible, referring to this python bug tracker thread

[编辑]使用os.system就像你在使用os.system这样的空间是我的知识不可能,指的是这个python bug跟踪器线程

A solution might be using the subprocess module insead.

解决方案可能是使用子进程模块insead。

import subprocess
subprocess.call(["C:/direc tory/program.exe", "-k", "C:/direc tory/program.exe"])