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

rcu*_*her 5 python python-2.7

当我执行像"net start"这样的简单命令时,我将成功输出,如下所示.

Python脚本:

import os

def test():
    cmd = ' net start  '
    output = os.popen(cmd).read()
    print output
test()
Run Code Online (Sandbox Code Playgroud)

输出:

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>
Run Code Online (Sandbox Code Playgroud)

但是当我执行长命令(例如:"net start"windows search")时,我没有得到任何输出.

Python脚本:

import os

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

test()
Run Code Online (Sandbox Code Playgroud)

输出:

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


C:\Users\test\Desktop\service>
Run Code Online (Sandbox Code Playgroud)

我试过"net start"windows搜索\"".也.但同样的问题.

有人可以指导我吗?

Ign*_*ams 3

文档中:

自 2.6 版起已弃用:此函数已过时。使用subprocess模块。特别检查用子流程模块替换旧功能部分。

subprocess.Popen(['net', 'start', 'windows search'], ...)
Run Code Online (Sandbox Code Playgroud)