最近,我一直在试用python来帮助我从python文件中运行Mac终端命令,特别是尝试查看我的设备上的蓝牙是打开还是关闭的。以下是我一直使用的代码:
#!/usr/bin/env python3
import os
import subprocess
p = subprocess.run('sudo defaults read /Library/Preferences/com.apple.Bluetooth ', shell=True, check=True, capture_output=True, text=True)
print(p.stdout, p.stderr)
但是,通常情况下,我会收到一个长输出,如下所示:
{
BluetoothAccStatus = 0;
BluetoothVersionNumber = 6;
ControllerPowerState = 1;
"D2D MAC Address" = {length = 6, bytes = 0x0200693eab66};
PersistentPorts = {
"incoming port - Bluetooth-Incoming-Port" = {
BTAuthenticationRequired = 0;
BTEncryptionType = 0;
BTName = "incoming port - Bluetooth-Incoming-Port";
BTPSM = 3;
BTRFCOMMChannel = 3;
BTSerialConnectionType = 0;
BTTTYName = "Bluetooth-Incoming-Port";
HiddenPort = 1;
IOTTYBaseName = "Bluetooth-Incoming-Port";
P49SerialPort = 1;
};
};
但是,我打算只获取该输出中的前两行,它们是BluetoothAccStatus(检查我的机器上是否打开或关闭了蓝牙)和BluetoothVersionNumber(混淆地显示了我安装的LMP版本的蓝牙)
是否有一个功能可以可靠地完成这项工作
你可以使用这行代码或者这个函数,在这里你可以输入你的整个段落,以及你想得到多少行作为输出
输出: 你想打多少线
一,
Python是由Guido van Rossum发明的
相关问题 更多 >
编程相关推荐