我正在尝试将 tshark(或任何与此相关的 shell 命令)写入文件。我试过使用 decode 和 encode 但它仍然对我大喊 split 方法不能使用数据类型。在“捕获停止”行之后,我的尝试仍在代码中作为注释。我也试过 r、a 和 a+ 作为开放模式,但我实际上使用这里使用的 ab+ 模式获得输出,所以我选择保留它。即使使用 a+ 模式也说“blah”是字节。我想将文件附加到输出中。谢谢!
import subprocess
import datetime
x="1"
x=input("Enter to continue. Input 0 to quit")
while x != "0":
#print("x is not zero")
blah = subprocess.check_output(["tshark -i mon0 -f \"subtype probe-req\" -T fields -e wlan.sa -e wlan_mgt.ssid -c 2"], shell=True)
with open("results.txt", 'ab+') as f:
f.write(blah)
x=input("To get out enter 0")
print("Capturing Stopped")
# blah.decode()
#blah = str.encode(blah)
#split the blah variable by line
splitblah …Run Code Online (Sandbox Code Playgroud)