TypeError a bytes-like object is required not str when writing to a file in Python3

0 votes

I have very recently switched to the Py 3.5 version but I noticed that the code mentioned below 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 switching to 3.5, I'm getting the following ERROR:-

TypeError: a bytes-like object is required, not 'str'
error on the last line (the pattern search code).

I have tried to use the .decode() function on either side of the statement along with the line mentioned below:

if tmp.find('some-pattern') != -1: continue

- to no avail.

I was able to resolve almost all 2:3 issues quickly, but this little statement is bugging me. Any solution for a way forward beyond this step?

Feb 11, 2022 in Python by Soham
• 9,710 points
1,967 views

1 answer to this question.

0 votes
After what I observed, you would have opened your file in the binary mode:-

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

This would state that all the data which the file is reading has been returned as bytes objects, not str. You will not be able to use a string in a containment test:
 

if 'some-pattern' in tmp: continue

Now you will have to use a bytes object to test against tmp instead:
 

if b'some-pattern' in tmp: continue

You could also open the file as a text file instead by replacing the 'rb' mode with 'r'.

answered Feb 11, 2022 by Rahul
• 9,680 points

Related Questions In Python

0 votes
0 answers

TypeError: 'type' object is not subscriptable when indexing in to a dictionary

I use dict to shorten things as ...READ MORE

May 24, 2022 in Python by Kichu
• 19,040 points
1,410 views
0 votes
1 answer

Raw_input method is not working in python3. How to use it?

raw_input is not supported anymore in python3. ...READ MORE

answered May 5, 2018 in Python by aayushi
• 750 points
3,380 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
2,904 views
0 votes
2 answers

How to check if a csv file is empty in pandas?

Try this: df = pd.DataFrame(columns=['Name', 'ID', 'Department']) if df.empty ...READ MORE

answered Jul 1, 2019 in Python by Bob
14,821 views
0 votes
1 answer

Open file in Python

There is this code I used for ...READ MORE

answered Nov 16, 2018 in Python by Jino
• 5,820 points
806 views
0 votes
1 answer

How to read numbers from file in Python?

Assuming you don't have extraneous whitespace: with open('file') ...READ MORE

answered Apr 16, 2019 in Python by SDeb
• 13,300 points
7,512 views
0 votes
0 answers

How to use string.replace() in python 3.x

The string.replace() is deprecated on python 3.x. ...READ MORE

Feb 18, 2022 in Python by Dev
• 6,000 points
408 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,020 points
1,699 views
0 votes
3 answers

'python' is not recognized as an internal or external command

Try "py" instead of "python" from command line: C:\Users\Cpsa>py Python 3.4.1 (v3.4.1:c0e311e010fc, May ...READ MORE

answered Feb 8, 2022 in Python by Rahul
• 9,680 points
2,553 views
0 votes
1 answer

Reverse a string in Python

You should try to use the following ...READ MORE

answered Feb 8, 2022 in Python by Rahul
• 9,680 points
708 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP