1

I have a text file with data such as

b'\x00\x09\x00\xfe'

This was piped into a text file from a TCP socket stream. Call this text file 'stream.txt'. I opened this file with the following code:

f = open("stream.txt", "rb")
bytes_read = f.read()

When I open this file within another Python script, I get a '\' for each '\' in the original file. On top of this, I cannot access the bytes array as such, since it appears to have become a string. That is, 'bytes_read' is now

'b"\\x00\\x09\\x00\\xfe"'

How can I recover this string as a bytes array?

The client code I used to capture this data is the following script:

from socket import *

clientsock = socket(AF_INET, SOCK_STREAM)
clientsock.connect(('1.2.3.4', 2000))      # Open the TCP socket

clientsock.sendall(b'myCommand')  # Send a command to the server
data = clientsock.recv(16)        # Wait for the response
print(data)                       # For piping to 'stream.txt'
clientsock.close()

As the data was printed to the terminal, I redirected it to a file:

$ python3 client.py > stream.txt

My goal is to bypass the redirect into text file and pipe directly into a plotter... But first I wanted to get this to work.

3
  • What did you do after bytes_read = f.read()? I don't quite get it. It just became string out of nowhere? Commented Apr 27, 2015 at 10:03
  • Your problem is, that Python handles your file right where you expect it to be handled wrong. Your file is not text, it's binary data aka. raw. You should not try to make text out of it. And you did that right by telling python to open the file in rb (read, binary) mode. Commented Apr 27, 2015 at 10:03
  • But I didn't make text out of it... unless I did so implicitly somewhere? Is there another way to capture that data (for example not using redirect from the the terminal)? Commented Apr 27, 2015 at 10:21

1 Answer 1

1

Was able to solve this by writing directly to a file. So rather than using 'print(data)', and redirecting to a file, I tried this:

file = open("rawData", "wb")
...
file.write(data)
...
file.close()

Was able to process "rawData" as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.