如何使用python运行cmd windows netsh命令? [英] How to run cmd windows netsh command using python?
本文介绍了如何使用python运行cmd windows netsh命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Windows 7 上运行以下 netsh 命令,但是它返回错误的语法
I am trying to run the following netsh command on Windows 7 however It returns incorrect 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
>>>
怎么了?
推荐答案
os.system
是一个非常古老的选择,并不真正推荐.
os.system
is a very old choice and not really recommended.
相反,您应该考虑 subprocess.call()
或 subprocess.Popen()
.
Instead you should consider subprocess.call()
or subprocess.Popen()
.
以下是它们的使用方法:
Here is how to use them:
如果你不关心输出,那么:
If you don't care about the output, then:
import subprocess
...
subprocess.call('netsh interface ipv4 set interface ""Wireless Network" metric=1', shell=True)
如果您确实关心输出,那么:
If you do care about the output, 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
这篇关于如何使用python运行cmd windows netsh命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文