我尝试了以下方法来打开命令提示符并运行示例命令。但它立即关闭:
import os
# as of now i am just passing cd /../, later will be changing to a different command
os.system("start /wait cmd /c {cd /../}")
Run Code Online (Sandbox Code Playgroud)
我也尝试过这种方式,但这会打开两个命令外壳:
import os
os.system("start /B start cmd.exe @cmd /k cd /d D:")
Run Code Online (Sandbox Code Playgroud)
是否可以只打开一个命令提示符并运行该命令?
我试图让python的subprocess.call方法通过列表(由一系列字符串组成)接受一些args命令,如python文档中所建议的那样.为了在将它放入我的实际脚本之前探索这种行为,我打开了IPython,运行了一些涉及shell设置和args命令的不同组合的命令,并得到以下行为:
In [3]: subprocess.call(['ls', '-%sl' %'a'])
total 320
drwxr-xr-x 20 Kohaugustine staff 680 Oct 15 16:55 .
drwxr-xr-x 5 Kohaugustine staff 170 Sep 12 17:16 ..
-rwxr-xr-x 1 Kohaugustine staff 8544 Oct 15 16:55 a.out
-rwxr-xr-x 1 Kohaugustine staff 8544 Oct 3 10:28 ex1-6
-rw-r--r--@ 1 Kohaugustine staff 204 Oct 3 10:28 ex1-6.c
-rwxr-xr-x 1 Kohaugustine staff 8496 Oct 3 10:15 ex1-7
-rw-r--r--@ 1 Kohaugustine staff 71 Oct 3 10:15 ex1-7.c
-rwxr-xr-x 1 Kohaugustine staff 8496 Sep 12 16:22 hello …Run Code Online (Sandbox Code Playgroud)