加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

在Windows 2.7.2中使用subprocess.call()的问题

发布时间:2020-12-14 04:31:38 所属栏目:Windows 来源:网络整理
导读:我正在尝试以下内容,并发生错误.我试图通过在控制台上调用 python从 Python shell /从脚本/ Windows控制台上运行它.没有什么似乎工作总是一样的错误. from subprocess import callpat = "d:info2.txt" call(["type",pat])Traceback (most recent call last)
我正在尝试以下内容,并发生错误.我试图通过在控制台上调用 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:Python27libsubprocess.py",line 493,in call
    return Popen(*popenargs,**kwargs).wait()
  File "C:Python27libsubprocess.py",line 679,in __init__
    errread,errwrite)
  File "C:Python27libsubprocess.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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读