我正在尝试以下内容,并发生错误.我试图通过在控制台上调用
python从
Python shell /从脚本/ Windows控制台上运行它.没有什么似乎工作总是一样的错误.
from subprocess import call >>>pat = "d:\info2.txt" >>> call(["type",pat]) >>>Traceback (most recent call last): File "<pyshell#56>",line 1,in <module> call(["type",pat]) File "C:\Python27\lib\subprocess.py",line 493,in call return Popen(*popenargs,**kwargs).wait() File "C:\Python27\lib\subprocess.py",line 679,in __init__ errread,errwrite) File "C:\Python27\lib\subprocess.py",line 893,in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified
有人知道这里有什么问题吗?
即使没有任何争论的简单调用([“date”]]也会失败,同样的错误.
我在用着 :
Python 2.72 32位版本在Windows 7机器上.
添加shell = True到
call:
>>> import subprocess >>> subprocess.call('dir',shell=True) 0
如你所见,它给出返回码的值,而不是dir的输出.此外,它等待命令完成,这样做
>>> subprocess.call('date',shell=True)
将等待您输入新的日期.
编辑:如果要捕获输出,请使用subprocess.check_output. DOS命令类型例如打印文件的内容.所以,假设你的文件info2.txt包含你的用户名,你可以这样做:
>>> import subprocess >>> path = r'd:\info2.txt' >>> output = subprocess.check_output(['type',path],shell=True) >>> print output Vinu
有关在Python中调用外部命令的所有方法,请参阅comprehensive overview to a related question,具体有关子进程的更多信息,请参见this article by Doug Hellmann.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。