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 in Python by Soham
• 8,730 points
75 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 by Rahul
• 8,980 points

Related Questions In Python

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
2,214 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,190 points
441 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
9,401 views
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 5, 2019 in Python by Shabnam
• 930 points
899 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,800 points
248 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
6,475 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 in Python by Dev
• 6,000 points
13 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,060 points
1,034 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 in Python by Rahul
• 8,980 points
543 views
0 votes
1 answer

Reverse a string in Python

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

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