How to run cmd windows netsh command using python?

I am trying to run the following netsh command on Windows 7 but it returns the wrong syntax

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.system("netsh interface ipv4 set interface ""Conexรฃo de Rede sem Fio"" metric=1") The syntax of the file name, directory name or volume label is incorrect. 1 >>> 

What's wrong?

+2
source share
1 answer

os.system is a very old choice and is not recommended.

Instead, you should consider subprocess.call() or subprocess.Popen() .

Here's how to use them:

If you do not care about the exit, then:

 import subprocess ... subprocess.call('netsh interface ipv4 set interface ""Wireless Network" metric=1', shell=True) 

If you care about the exit, then:

 netshcmd=subprocess.Popen('netsh interface ipv4 set interface ""Wireless Network" metric=1', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE ) output, errors = netshcmd.communicate() if errors: print "WARNING: ", errors else: print "SUCCESS ", output 
+4
source

All Articles