【发布时间】:2017-02-04 14:34:09
【问题描述】:
在我的旧 python 脚本中,我使用以下代码来显示 Windows cmd 命令的结果:
print(os.popen("dir c:\\").read())
正如 python 2.7 文档所说,os.popen 已过时,建议使用subprocess。我按照以下文档进行操作:
result = subprocess.Popen("dir c:\\").stdout
我收到错误消息:
WindowsError: [Error 2] The system cannot find the file specified
你能告诉我使用subprocess模块的正确方法吗?
【问题讨论】:
-
请注意,Windows 上的
dir内置在 shell 中,因此它不是独立的可执行文件 - 请参阅 stackoverflow.com/questions/20330385/… -
@metatoaster 谢谢。看完帖子,我的理解是
subprocess不能调用内置的shell命令。那么os.popen在这种情况下是不是“过时”?
标签: python windows python-2.7 command-line subprocess