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'
.
Related Posts:
- How to open a file using the open with statement
- How to read a file line-by-line into a list?
- Best way to convert string to bytes in Python 3?
- Best way to convert string to bytes in Python 3?
- TypeError: not all arguments converted during string formatting python
- Convert bytes to a string
- How to print like printf in Python3?
- How can I concatenate str and int objects?
- Convert bytes to int?
- How do I format a string using a dictionary in python-3.x?
- How to save a dictionary to a file?
- Replacing a character from a certain index
- How to convert ‘binary string’ to normal string in Python3?
- Properly formatted multiplication table
- How to compare individual characters in two strings in Python 3
- What is the difference between a string and a byte string?
- Python cant convert ‘list’ object to str error [closed]
- Using strip() to clean up a string
- Python – difference between two strings
- How do I copy a file in Python?
- Use Gif Logo For Loading Screen In Kivy
- How can I make a time delay in Python? [duplicate]
- Python – TypeError: ‘int’ object is not iterable
- Does Python have a string ‘contains’ substring method?
- Python: ‘ModuleNotFoundError’ when trying to import module from imported package
- PermissionError: [Errno 13] Permission denied
- What is setup.py?
- Do I understand os.walk right?
- Invalid character in identifier
- ‘str’ object does not support item assignment
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- FileNotFoundError: [Errno 2] No such file or directory
- Writing a list to a file with Python
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- Invalid character in identifier
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- How to find all the indexes of a recurring item in a list?
- How to find all occurrences of a substring?
- How do you use subprocess.check_output() in Python?
- How do order of operations go on Python?
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- Python TypeError: not enough arguments for format string
- Pinging servers in Python
- Import error: No module name urllib2
- Add Legend to Seaborn point plot
- What does the ‘b’ character do in front of a string literal?
- Best way to replace multiple characters in a string?
- How can I install Python’s pip3 on my Mac?
- Python: Get the first character of the first string in a list?
- How to convert string to binary?
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- Get ZeroDivisionError: float division in python
- Where does this come from: -*- coding: utf-8 -*-
- Open file in a relative location in Python
- Remove all special characters, punctuation and spaces from string
- How to delete last item in list?
- Don’t understand this SyntaxError: illegal target for annotation
- Using Queue in python
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
- ValueError: cannot index with vector containing NA / NaN values
- SyntaxError invalid token
- How to check if type of a variable is string?
- For loop and ‘numpy.float64’ object is not iterable error
- python mpl_toolkits installation issue
- Shebang doesn’t work with python3
- TypeError: ‘dict_keys’ object does not support indexing
- How can I check if character in a string is a letter? (Python)
- How to strip all whitespace from string
- Python Binomial Coefficient
- pythonw.exe or python.exe?
- Find the nth occurrence of substring in a string
- python3 TypeError: ‘function’ object is not iterable
- How to start from second index for for-loop
- Converting a sentence to piglatin in Python
- Python: Converting string into decimal number
- Python Error – TypeError: input expected at most 1 arguments, got 3 [duplicate]
- A general tree implementation?
- How to convert a file into a dictionary?
- ModuleNotFoundError: No module named ‘seaborn’ in Python IDE
- Is there a ceiling equivalent of // operator in Python?
- TypeError: ‘set’ object does not support indexing
- Root mean square of a function in python
- Checking to see if a string is an integer or float
- pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding’ when trying to install new packages
- Split string using a newline delimiter with Python
- TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
- How to know/change current directory in Python shell?
- What is an easy way to implement fprintf in python?
- How to use cmp() in Python 3?
- Generator expression must be parenthesized if not sole argument
- Does python have header files like C/C++?
- Error: No module named ‘fcntl’
- Python can’t find file
- TypeError: got multiple values for argument
- IOError: [Errno 32] Broken pipe when piping: `prog.py | othercmd`
- Python webbrowser.open() to open Chrome browser
- Find index of last occurrence of a substring in a string
- Python convert tuple to string
- TypeError: coercing to Unicode: need string or buffer