编程语言
首页 > 编程语言> > python – 关于为什么os.system()可能失败的任何想法?

python – 关于为什么os.system()可能失败的任何想法?

作者:互联网

我最近在一台新电脑上安装了python.
我找不到os.system()失败或失败的原因.
我在主脚本中尝试了os.popen()和subprocess,但这并没有解决问题.

最关心的是为什么这个简单的os.system()测试无法开始.

在cmd提示符下:

>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v. 1500 32 bit (Intel)] on win32
>>> import os
>>> os.system("dir")
-1
>>> os.system("cmd /c dir")
-1
>>> import subprocess
>>> subprocess.call("dir", shell=True)
Traceback ...
...
WindowsError: [Error 2] System could not locate file
>>> exit()
dir
(list of files)

我使用dir作为示例,但是当我调用系统路径中包含的任何其他程序时,会发生同样的事情.它将直接从cmd行运行,但不能通过os.system或subprocess运行.

解决方法:

dir不是可执行文件,它是一个cmd.exe命令.

尝试:

os.system("cmd /c dir")

要么

subprocess.call("dir", shell=True)

标签:python,cmd,os-system
来源: https://codeday.me/bug/20190626/1290024.html