python - 如何使用python运行cmd windows netsh命令?

标签 python windows

我尝试在 Windows 7 上运行以下 netsh 命令,但它返回错误的语法

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是一个非常古老的选择,并不真正推荐。

相反,您应该考虑 subprocess.call()subprocess.Popen()

以下是如何使用它们:

如果你不关心输出,那么:

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

如果您确实关心输出,那么:

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 - 如何使用python运行cmd windows netsh命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12381733/

相关文章:

python - 如何在 Windows 上使用信号退出功能?

python - 具有多个根的编程语言

python - 一次用于多列的 Pandas 数据透视表

python - Python 中正则表达式的替代品

c++ - en_US.UTF-8 语言环境的 Windows 等效项是什么?

c++ - 如何确定阻塞的 SSL BIO 连接是否已关闭?

c# - 如何防止我的应用创建新实例

windows - DOS下如何重命名当前目录下的所有隐藏目录?

python - 交互式 Python : cannot get `%lprun` to work, 尽管 line_profiler 已正确导入

python - 嵌套(二维)字典的类迭代器?