【发布时间】:2013-12-18 06:49:31
【问题描述】:
在安装了 32 位 python 2.7 的 64 位系统中,我正在尝试执行以下操作:
import subprocess
p = subprocess.call('dir', shell=True)
print p
但这给了我:
Traceback (most recent call last):
File "test.py", line 2, in <module>
p = subprocess.call('dir', shell=True)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
如果我在终端做...
dir
...它当然会打印当前文件夹的内容。
我已尝试将 shell 参数更改为 shell=False。
编辑:实际上我无法使用subprocess.call() 调用路径上的任何可执行文件。声明 p = subprocess.call('dir', shell=True) 在另一台机器上运行良好,我认为它是相关的。
如果我这样做
subprocess.call('PATH', shell=True)
然后我得到
Traceback (most recent call last):
File "test.py", line 4, in <module>
subprocess.call('PATH', shell=True)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
如果我这样做:
import os
print os.curdir
然后我得到
.
以上所有操作都在以管理员模式启动的终端中执行。
【问题讨论】:
-
dir不是可执行文件,它是一个仅限控制台的命令。为什么不改用os.listdir()? -
奇怪,代码在 ubuntu、rhel、win xp、win 7 上对我有用
-
实际上我无法调用路径上的任何可执行文件。声明
p = subprocess.call('dir', shell=True)在另一台机器上运行良好,我认为它是相关的。我想尽可能地简化我的问题,所以我只提出了带有“dir”的示例。我更新了问题... -
您是从控制台还是从某个 IDE 运行脚本?
-
感谢您迄今为止的所有帮助。我很感激!我已经根据您的想法更新了我的问题。
标签: python shell python-2.7 path subprocess