如何使用双引号执行命令(net start" windows search")使用python' os'模块?

时间:2015-10-11 18:51:47

标签: python python-2.7

当我执行" net start"之类的简单命令时,我正在成功输出输出,如下所示。

Python脚本:

import os

def test():
    cmd = ' net start  '
    output = os.popen(cmd).read()
    print output
test()

输出:

C:\Users\test\Desktop\service>python test.py
These Windows services are started:

   Application Experience
   Application Management
   Background Intelligent Transfer Service
   Base Filtering Engine
   Task Scheduler
   TCP/IP NetBIOS Helper


The command completed successfully.

C:\Users\test\Desktop\service>

但是当我执行长命令时(例如:" net start" windows search")我 NOT 获得任何输出。

Python脚本:

import os

def test():
    cmd = ' net start "windows search"  '
    output = os.popen(cmd).read()
    print output

test()

输出:

C:\Users\test\Desktop\service>python test.py


C:\Users\test\Desktop\service>

我试过" net start \" windows search \" &#34 ;.也。但同样的问题。

有人可以指导我吗?

1 个答案:

答案 0 :(得分:3)

来自the documentation

  

从2.6版开始不推荐使用:此功能已过时。使用subprocess模块。请特别检查Replacing Older Functions with the subprocess Module部分。

subprocess.Popen(['net', 'start', 'windows search'], ...)