• Register
search
Log In
0 votes
719 views

Problem :

I am new  to Py 3.5. This code was working properly in Python 2.7:

with open(fname, 'rb') as f:
    lines = [x.strip() for x in f.readlines()]

for line in lines:
    tmp = line.strip().lower()
    if 'some-pattern' in tmp: continue
    # ... code

After upgrading to 3.5, I'm getting the:

TypeError: a bytes-like object is required, not 'str'
6.9k points

2 Answers

0 votes

Solution :

It seems you opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data is read from the file  returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

Further Readings:

https://github.com/mitsuhiko/phpserialize/issues/15

36.1k points
edited by
0 votes

Solution:

You opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

You can get rid of the error by using the below-mentioned way:-

And the reason you are getting that error is in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes strings instead. So... a couple of suggestions:

  1. The first thing you can use is c.sendall() method instead of c.send() to prevent possible issues where you may not have sent the entire msg with one call.

  2. For literals, add a 'b' for bytes string: c.sendall(b'Thank you for connecting')

  3. For variables, you need to encode Unicode strings to byte strings (see below)

Below is the code which will work both on Python 2.0 as well as Python 3.0:-
output = 'Thank you for connecting'

c.sendall(output.encode('utf-8'))

 

12.8k points

Related questions

0 votes
1 answer 987 views
987 views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
1 answer 12 views
12 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11 samhaz 5k points
0 votes
1 answer 127 views
127 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2 alecxe 7.5k points
1 vote
2 answers 62 views
62 views
Problem : The program supposed to take in two names, and if they are the same length it should check if they are the same word. But i am getting below error in python python typeerror: not all arguments converted during string formatting
asked Oct 21, 2019 peterlaw 6.9k points
1 vote
1 answer 18 views
18 views
Problem : I am learning to use Python. Currently I want to iterate through the files which are present in the folder on the local machine and then want to upload only those files whose file names have "Service_Areas" to the required FTP site with the use of ... rb')) TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader Please guide me in fixing my python ftp related error.
asked May 14 Martin K 6.6k points