I am trying to write a tshark (or any shell command for that matter) to a file. I've tried using decode and encode but it still yells at me that the split method cannot use the datatype. My attempts are still in the code as comments, after the "capturing stopped" line. I have also tried r, a and a+ as the open modes but I actually get output with the ab+ mode used here so I opted to keep it. Even using a+ mode said "blah" was bytes. I would like to keep the file appended with the output. Thank you!
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 = blah.split("\n")
#repeat for each line, -1 ignores first line since it contains headers
for value in splitblah[:-1]:
#split each line by tab delimiter
splitvalue = value.split("\t")
#Assign variables to split fields
MAC = str(splitvalue[1])
SSID = str(splitvalue[2])
time = str(datetime.datetime.now())
#write and format output to results file
with open("results.txt", "ab+") as f:
f.write(MAC+" "+SSID+" "+time+"\r\n")
Typeerror a bytes like object is required not str error occurs when we compare any 'str' object with the 'byte' type object. The best way to fix this error is to convert them into 'str' before comparison or any other operation.
Bytes-like object in python In Python, a string object is a series of characters that make a string. In the same manner, a byte object is a sequence of bits/bytes that represent data. Strings are human-readable while bytes are computer-readable. Data is converted into byte form before it is stored on a computer.
Bytes-like objects are objects that are stored using the bytes data type. Bytes-like objects are not strings and so they cannot be manipulated like a string.
The error “typeerror: a bytes-like object is required, not ‘str’” is raised when you treat an object as a string instead of as a series of bytes. A common scenario in which this error is raised is when you read a text file as a binary.
Bytes-like objects are not strings and so they cannot be manipulated like a string. This error is commonly raised when you open a file as a binary file instead of as a text file.
But the same code when executed in Python 3 will throw the error - typeerror: a bytes-like object is required, not 'str'. This is because, in Python 2, the strings are by default treated as bytes. The original strings in Python 2 are 8-bit strings, which play a crucial role while working with byte sequences and ASCII text.
You can achieve this by using the prefix b before the delimiter string within the split () function. This allows you to operate upon a byte object within the split () function, thereby avoiding the TypeError. In our code, we are trying to read the file in binary mode and then creating a list of bytes.
If your question boils down to this:
I've tried using decode and encode but it still yells at me that the split method cannot use the datatype.
The error at hand can be demonstrated by the following code:
>>> blah = b'hello world' # the "bytes" produced by check_output
>>> blah.split('\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
In order to split bytes
, a bytes
object must also be provided. The fix is simply:
>>> blah.split(b'\n')
[b'hello world']
Use decode()
correctly: either in two steps (if you want to reuse blah
):
blah = blah.decode()
splitblah = blah.split("\n")
# other code that uses blah
or inline (if you need it for single use):
splitblah = blah.decode().split("\n")
Your issue with using decode()
was that you did not use its return value. Note that decode()
does not change the object (blah
) to assign or pass it to something:
# WRONG!
blah.decode()
SEE ALSO:decode
docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With