Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute commands with double quotes (net start "windows search") using python 'os' module?

When I execute simple command like "net start", I am getting output successfully as shown below.

Python script:

import os

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

Output:

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>

But When I execute long commands (for example : "net start "windows search") I am NOT getting any output.

Python script:

import os

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

test()

Output:

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


C:\Users\test\Desktop\service>

I have tried "net start \"windows search\" ". also. But same issue.

Can anyone guide me on this please?

like image 950
rcubefather Avatar asked Oct 19 '22 00:10

rcubefather


1 Answers

From the documentation:

Deprecated since version 2.6: This function is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

subprocess.Popen(['net', 'start', 'windows search'], ...)
like image 85
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 10:10

Ignacio Vazquez-Abrams