![](/img/trans.png)
[英]How to run python script without writing command on cmd again and again
[英]How do I run following cmd command in python without error?
我嘗試在 python 中獲取網絡適配器名稱(例如“以太網”)。 我找到並工作的唯一方法是輸入
for /F "skip=3 tokens=3*" %G in ('netsh interface show interface') do echo %H
在windows CMD中。所以我嘗試了以下操作:
os.system("for /F \"skip=3 tokens=3*\" %G in (\"netsh interface show interface\")")
但即使我修復了字符串引用的東西,它也會給出語法錯誤......子進程模塊也不起作用。 有人有想法嗎?
您在使用, shell=True
子進程參數時遇到問題。 您不想看到bash
試圖解釋該命令。
將長命令放入ether.bat
,這樣您就不會因引用問題而分心,並且可以專注於bash
或cmd.exe
是否正在運行您的批處理文件。
這應該足夠了:
os.system('cmd ether.bat')
雖然最好使用這個接口:
interface = subprocess.check_output(['cmd', 'ether.bat'])
Python 提供了非常好的文本解析工具。 考慮放棄 cmd.exe 並只運行netsh interface show interface
,使用 python 循環來理解返回的文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.