The reason why you always got True
has already been given, so I’ll just offer another suggestion:
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
with open('example.txt') as f:
if 'blabla' in f.read():
print("true")
Another trick: you can alleviate the possible memory problems by using mmap.mmap()
to create a “string-like” object that uses the underlying file (instead of reading the whole file in memory):
import mmap
with open('example.txt') as f:
s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
if s.find('blabla') != -1:
print('true')
NOTE: in python 3, mmaps behave like bytearray
objects rather than strings, so the subsequence you look for with find()
has to be a bytes
object rather than a string as well, eg. s.find(b'blabla')
:
#!/usr/bin/env python3
import mmap
with open('example.txt', 'rb', 0) as file,
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(b'blabla') != -1:
print('true')
You could also use regular expressions on mmap
e.g., case-insensitive search: if re.search(br'(?i)blabla', s):
In this Python tutorial, you’ll learn to search a string in a text file. Also, we’ll see how to search a string in a file and print its line and line number.
After reading this article, you’ll learn the following cases.
- If a file is small, read it into a string and use the
find()
method to check if a string or word is present in a file. (easier and faster than reading and checking line per line) - If a file is large, use the mmap to search a string in a file. We don’t need to read the whole file in memory, which will make our solution memory efficient.
- Search a string in multiple files
- Search file for a list of strings
We will see each solution one by one.
Table of contents
- How to Search for a String in Text File
- Example to search for a string in text file
- Search file for a string and Print its line and line number
- Efficient way to search string in a large text file
- mmap to search for a string in text file
- Search string in multiple files
- Search file for a list of strings
How to Search for a String in Text File
Use the file read()
method and string class find()
method to search for a string in a text file. Here are the steps.
- Open file in a read mode
Open a file by setting a file path and access mode to the
open()
function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing. For example, r is for reading.fp= open(r'file_path', 'r')
- Read content from a file
Once opened, read all content of a file using the
read()
method. Theread()
method returns the entire file content in string format. - Search for a string in a file
Use the
find()
method of a str class to check the given string or word present in the result returned by theread()
method. Thefind()
method. The find() method will return -1 if the given text is not present in a file - Print line and line number
If you need line and line numbers, use the
readlines(
) method instead ofread()
method. Use the for loop andreadlines()
method to iterate each line from a file. Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number
Example to search for a string in text file
I have a ‘sales.txt’ file that contains monthly sales data of items. I want the sales data of a specific item. Let’s see how to search particular item data in a sales file.
def search_str(file_path, word):
with open(file_path, 'r') as file:
# read all content of a file
content = file.read()
# check if string present in a file
if word in content:
print('string exist in a file')
else:
print('string does not exist in a file')
search_str(r'E:demosfiles_demosaccountsales.txt', 'laptop')
Output:
string exists in a file
Search file for a string and Print its line and line number
Use the following steps if you are searching a particular text or a word in a file, and you want to print a line number and line in which it is present.
- Open a file in a read mode.
- Next, use the
readlines()
method to get all lines from a file in the form of a list object. - Next, use a loop to iterate each line from a file.
- Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number.
Example: In this example, we’ll search the string ‘laptop’ in a file, print its line along with the line number.
# string to search in file
word = 'laptop'
with open(r'E:demosfiles_demosaccountsales.txt', 'r') as fp:
# read all lines in a list
lines = fp.readlines()
for line in lines:
# check if string present on a current line
if line.find(word) != -1:
print(word, 'string exists in file')
print('Line Number:', lines.index(line))
print('Line:', line)
Output:
laptop string exists in a file line: laptop 10 15000 line number: 1
Note: You can also use the readline()
method instead of readlines()
to read a file line by line, stop when you’ve gotten to the lines you want. Using this technique, we don’t need to read the entire file.
Efficient way to search string in a large text file
All above way read the entire file in memory. If the file is large, reading the whole file in memory is not ideal.
In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.
- Open a file in read mode
- Use for loop with
enumerate()
function to get a line and its number. Theenumerate()
function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by theopen()
function to theenumerate()
. - We can use this enumerate object with a for loop to access the each line and line number.
Note: The enumerate(file_pointer)
doesn’t load the entire file in memory, so this is an efficient solution.
Example:
with open(r"E:demosfiles_demosaccountsales.txt", 'r') as fp:
for l_no, line in enumerate(fp):
# search string
if 'laptop' in line:
print('string found in a file')
print('Line Number:', l_no)
print('Line:', line)
# don't look for next lines
break
Example:
string found in a file Line Number: 1 Line: laptop 10 15000
mmap to search for a string in text file
In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.
Also, you can use the mmap module to find a string in a huge file. The mmap.mmap()
method creates a bytearray
object that checks the underlying file instead of reading the whole file in memory.
Example:
import mmap
with open(r'E:demosfiles_demosaccountsales.txt', 'rb', 0) as file:
s = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
if s.find(b'laptop') != -1:
print('string exist in a file')
Output:
string exist in a file
Search string in multiple files
Sometimes you want to search a string in multiple files present in a directory. Use the below steps to search a text in all files of a directory.
- List all files of a directory
- Read each file one by one
- Next, search for a word in the given file. If found, stop reading the files.
Example:
import os
dir_path = r'E:demosfiles_demosaccount'
# iterate each file in a directory
for file in os.listdir(dir_path):
cur_path = os.path.join(dir_path, file)
# check if it is a file
if os.path.isfile(cur_path):
with open(cur_path, 'r') as file:
# read all content of a file and search string
if 'laptop' in file.read():
print('string found')
break
Output:
string found
Search file for a list of strings
Sometimes you want to search a file for multiple strings. The below example shows how to search a text file for any words in a list.
Example:
words = ['laptop', 'phone']
with open(r'E:demosfiles_demosaccountsales.txt', 'r') as f:
content = f.read()
# Iterate list to find each word
for word in words:
if word in content:
print('string exist in a file')
Output:
string exist in a file
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
In this article, we are going to see how to search for a string in text files using Python
Example:
string = “GEEK FOR GEEKS”
Input: “FOR”
Output: Yes, FOR is present in the given string.
Text File for demonstration:
myfile.txt
Finding the index of the string in the text file using readline()
In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.
Python3
with
open
(r
'myfile.txt'
,
'r'
) as fp:
lines
=
fp.readlines()
for
row
in
lines:
word
=
'Line 3'
if
row.find(word) !
=
-
1
:
print
(
'string exists in file'
)
print
(
'line Number:'
, lines.index(row))
Output:
string exists in file line Number: 2
Finding string in a text file using read()
we are going to search string line by line if the string is found then we will print that string and line number using the read() function.
Python3
with
open
(r
'myfile.txt'
,
'r'
) as
file
:
content
=
file
.read()
if
'Line 8'
in
content:
print
(
'string exist'
)
else
:
print
(
'string does not exist'
)
Output:
string does not exist
Search for a String in Text Files using enumerate()
We are just finding string is present in the file or not using the enumerate() in Python.
Python3
with
open
(r
"myfile.txt"
,
'r'
) as f:
for
index, line
in
enumerate
(f):
if
'Line 3y'
in
line:
print
(
'string found in a file'
)
break
print
(
'string does not exist in a file'
)
Output:
string does not exist in a file
Last Updated :
14 Mar, 2023
Like Article
Save Article
I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.
What is the best way to do this, within the following code?
f = open(file)
for line in f:
if line.contains('foo'):
newline = line.replace('foo', 'bar')
# how to write this newline back to the file
SilentGhost
304k66 gold badges305 silver badges292 bronze badges
asked Sep 2, 2008 at 9:19
0
The shortest way would probably be to use the fileinput module. For example, the following adds line numbers to a file, in-place:
import fileinput
for line in fileinput.input("test.txt", inplace=True):
print('{} {}'.format(fileinput.filelineno(), line), end='') # for Python 3
# print "%d: %s" % (fileinput.filelineno(), line), # for Python 2
What happens here is:
- The original file is moved to a backup file
- The standard output is redirected to the original file within the loop
- Thus any
print
statements write back into the original file
fileinput
has more bells and whistles. For example, it can be used to automatically operate on all files in sys.args[1:]
, without your having to iterate over them explicitly. Starting with Python 3.2 it also provides a convenient context manager for use in a with
statement.
While fileinput
is great for throwaway scripts, I would be wary of using it in real code because admittedly it’s not very readable or familiar. In real (production) code it’s worthwhile to spend just a few more lines of code to make the process explicit and thus make the code readable.
There are two options:
- The file is not overly large, and you can just read it wholly to memory. Then close the file, reopen it in writing mode and write the modified contents back.
- The file is too large to be stored in memory; you can move it over to a temporary file and open that, reading it line by line, writing back into the original file. Note that this requires twice the storage.
Alex Altair
3,1063 gold badges20 silver badges37 bronze badges
answered Nov 14, 2008 at 15:47
Eli BenderskyEli Bendersky
261k88 gold badges350 silver badges412 bronze badges
16
I guess something like this should do it. It basically writes the content to a new file and replaces the old file with the new file:
from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove
def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
#Copy the file permissions from the old file to the new file
copymode(file_path, abs_path)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
answered Sep 2, 2008 at 9:42
Thomas WatnedalThomas Watnedal
4,8834 gold badges23 silver badges23 bronze badges
11
Here’s another example that was tested, and will match search & replace patterns:
import fileinput
import sys
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
Example use:
replaceAll("/fooBar.txt","HellosWorld!$","GoodbyesWorld.")
the Tin Man
158k41 gold badges214 silver badges302 bronze badges
answered Nov 24, 2008 at 19:02
JasonJason
2,69127 silver badges28 bronze badges
8
This should work: (inplace editing)
import fileinput
# Does a list of files, and
# redirects STDOUT to the file in question
for line in fileinput.input(files, inplace = 1):
print line.replace("foo", "bar"),
Gringo Suave
29.5k6 gold badges88 silver badges75 bronze badges
answered Sep 7, 2009 at 10:07
KinlanKinlan
16.3k5 gold badges56 silver badges88 bronze badges
4
Based on the answer by Thomas Watnedal.
However, this does not answer the line-to-line part of the original question exactly. The function can still replace on a line-to-line basis
This implementation replaces the file contents without using temporary files, as a consequence file permissions remain unchanged.
Also re.sub instead of replace, allows regex replacement instead of plain text replacement only.
Reading the file as a single string instead of line by line allows for multiline match and replacement.
import re
def replace(file, pattern, subst):
# Read contents from file as a single string
file_handle = open(file, 'r')
file_string = file_handle.read()
file_handle.close()
# Use RE package to allow for replacement (also allowing for (multiline) REGEX)
file_string = (re.sub(pattern, subst, file_string))
# Write contents to file.
# Using mode 'w' truncates the file.
file_handle = open(file, 'w')
file_handle.write(file_string)
file_handle.close()
answered Nov 30, 2012 at 8:51
ThijsThijs
2593 silver badges4 bronze badges
2
As lassevk suggests, write out the new file as you go, here is some example code:
fin = open("a.txt")
fout = open("b.txt", "wt")
for line in fin:
fout.write( line.replace('foo', 'bar') )
fin.close()
fout.close()
answered Sep 2, 2008 at 9:42
hamishmcnhamishmcn
7,81310 gold badges40 silver badges46 bronze badges
If you’re wanting a generic function that replaces any text with some other text, this is likely the best way to go, particularly if you’re a fan of regex’s:
import re
def replace( filePath, text, subs, flags=0 ):
with open( filePath, "r+" ) as file:
fileContents = file.read()
textPattern = re.compile( re.escape( text ), flags )
fileContents = textPattern.sub( subs, fileContents )
file.seek( 0 )
file.truncate()
file.write( fileContents )
answered Feb 18, 2014 at 14:43
0
A more pythonic way would be to use context managers like the code below:
from tempfile import mkstemp
from shutil import move
from os import remove
def replace(source_file_path, pattern, substring):
fh, target_file_path = mkstemp()
with open(target_file_path, 'w') as target_file:
with open(source_file_path, 'r') as source_file:
for line in source_file:
target_file.write(line.replace(pattern, substring))
remove(source_file_path)
move(target_file_path, source_file_path)
You can find the full snippet here.
formatkaka
1,2383 gold badges13 silver badges26 bronze badges
answered Sep 7, 2013 at 18:39
KiranKiran
1,0471 gold badge11 silver badges24 bronze badges
1
fileinput
is quite straightforward as mentioned on previous answers:
import fileinput
def replace_in_file(file_path, search_text, new_text):
with fileinput.input(file_path, inplace=True) as file:
for line in file:
new_line = line.replace(search_text, new_text)
print(new_line, end='')
Explanation:
fileinput
can accept multiple files, but I prefer to close each single file as soon as it is being processed. So placed singlefile_path
inwith
statement.print
statement does not print anything wheninplace=True
, becauseSTDOUT
is being forwarded to the original file.end=''
inprint
statement is to eliminate intermediate blank new lines.
You can used it as follows:
file_path = '/path/to/my/file'
replace_in_file(file_path, 'old-text', 'new-text')
answered Oct 3, 2019 at 10:37
AkifAkif
5,7082 gold badges41 silver badges43 bronze badges
1
Create a new file, copy lines from the old to the new, and do the replacing before you write the lines to the new file.
answered Sep 2, 2008 at 9:24
Lasse V. KarlsenLasse V. Karlsen
378k101 gold badges625 silver badges820 bronze badges
Expanding on @Kiran’s answer, which I agree is more succinct and Pythonic, this adds codecs to support the reading and writing of UTF-8:
import codecs
from tempfile import mkstemp
from shutil import move
from os import remove
def replace(source_file_path, pattern, substring):
fh, target_file_path = mkstemp()
with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
for line in source_file:
target_file.write(line.replace(pattern, substring))
remove(source_file_path)
move(target_file_path, source_file_path)
answered May 2, 2014 at 11:15
igniteflowigniteflow
8,26610 gold badges38 silver badges46 bronze badges
1
Using hamishmcn’s answer as a template I was able to search for a line in a file that match my regex and replacing it with empty string.
import re
fin = open("in.txt", 'r') # in file
fout = open("out.txt", 'w') # out file
for line in fin:
p = re.compile('[-][0-9]*[.][0-9]*[,]|[-][0-9]*[,]') # pattern
newline = p.sub('',line) # replace matching strings with empty string
print newline
fout.write(newline)
fin.close()
fout.close()
answered Apr 17, 2014 at 2:13
EmmanuelEmmanuel
431 silver badge5 bronze badges
1
if you remove the indent at the like below, it will search and replace in multiple line.
See below for example.
def replace(file, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
print fh, abs_path
new_file = open(abs_path,'w')
old_file = open(file)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(file)
#Move new file
move(abs_path, file)
answered Aug 2, 2012 at 19:12
1
In this article, we will learn to search and replace a text of a file in Python. We will use some built-in functions and some custom codes as well. We will replace text, or strings within a file using mentioned ways.
Python provides multiple built-in functions to perform file handling operations. Instead of creating a new modified file, we will search a text from a file and replace it with some other text in the same file. This modifies the file with new data. This will replace all the matching texts within a file and decreases the overhead of changing each word. Let us discuss some of the mentioned ways to search and replace text in a file in Python.
Sample Text File
We will use the below review.text file to modify the contents.
In the movie Ghost
the joke is built on a rock-solid boundation
the movie would still work played perfectly straight
The notion of a ghost-extermination squad taking on
the paramal hordes makes a compelling setup for a big-budget adventure of any stripe
Indeed, the film as it stands frequently allows time to pass without a gag
But then comes the punch line: the characters are funny
And because we’ve been hooked by the story, the humor the characters provide is all the richer.
Example: Use replace() to Replace a Text within a File
The below example uses replace()
function to modify a string within a file. We use the review.txt file to modify the contents. It searches for the string by using for loop and replaces the old string with a new string.
open(file,'r')
– It opens the review.txt file for reading the contents of the file.
strip()
– While iterating over the contents of the file, strip() function strips the end-line break.
replace(old,new)
– It takes an old string and a new string to replace its arguments.
file.close()
– After concatenating the new string and adding an end-line break, it closes the file.
open(file,'w')
– It opens the file for writing and overwrites the old file content with new content.
reading_file = open("review.txt", "r")
new_file_content = ""
for line in reading_file:
stripped_line = line.strip()
new_line = stripped_line.replace("Ghost", "Ghostbusters")
new_file_content += new_line +"n"
reading_file.close()
writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
Output:
Example: Replace a Text using Regex Module
An alternative method to the above-mentioned methods is to use Python’s regex
module. The below example imports regex module. It creates a function and passed a file, an old string, and a new string as arguments. Inside the function, we open the file in both read and write mode and read the contents of the file.
compile()
– It is used to compile a regular expression pattern and convert it into a regular expression object which can then be used for matching.
escape()
– It is used to escape special characters in a pattern.
sub()
– It is used to replace a pattern with a string.
#importing the regex module
import re
#defining the replace method
def replace(filePath, text, subs, flags=0):
with open(file_path, "r+") as file:
#read the file contents
file_contents = file.read()
text_pattern = re.compile(re.escape(text), flags)
file_contents = text_pattern.sub(subs, file_contents)
file.seek(0)
file.truncate()
file.write(file_contents)
file_path="review.txt"
text="boundation"
subs="foundation"
#calling the replace method
replace(file_path, text, subs)
Output:
FileInput in Python
FileInput
is a useful feature of Python for performing various file-related operations. For using FileInput, fileinput
module is imported. It is great for throwaway scripts. It is also used to replace the contents within a file. It performs searching, editing, and replacing in a text file. It does not create any new files or overheads.
Syntax-
FileInput(filename, inplace=True, backup='.bak')
Parameters-
backup
– The backup is an extension for the backup file created before editing.
Example: Search and Replace a Text using FileInput and replace() Function
The below function replaces a text using replace()
function.
import fileinput
filename = "review.txt"
with fileinput.FileInput(filename, inplace = True, backup ='.bak') as f:
for line in f:
if("paramal" in line):
print(line.replace("paramal","paranormal"), end ='')
else:
print(line, end ='')
Output:
Conclusion
In this article, we learned to search and replace a text, or a string in a file by using several built-in functions such as replace()
, regex
and FileInput
module. We used some custom codes as well. We saw outputs too to differentiate between the examples. Therefore, to search and replace a string in Python user can load the entire file and then replaces the contents in the same file instead of creating a new file and then overwrite the file.