Count a string in a text file with Python

I am a beginner in Python. I am trying to count a specific character in a text file (a string). I manage to read in the file and with a for loop I try to loop through each row in the text file and find a specific character. The goal is to print out the count of that character. Unfortunately I get 0 as a result although I have a couple of that character in the file.

in_file= open("file_x.txt")
lines = in_file.readlines()

d=0

for line in in_file:
    if d in line:
        d=d+1
print(d)

Any suggestion? Thank you

Answer

You have a couple problems.

First, when reading a file you should specify the mode (not strictly needed, but greeatly helps clarify intent). In this case for reading do:

open("file_x.txt", "r")

Next, when reading a file you need to make sure you close it after you’re done. You should use with for this:

with open("file_x.txt", "r") as in_file:
    lines = in_file.readlines()

You didn’t loop the lines, you looped the already read file in_file. Change to:

for line in lines:

You didn’t use a string to check the line, you used the variable d which is an int 0. Change to "d"

if "d" in line:

All together now:

with open("file_x.txt", "r") as in_file:
    lines = in_file.readlines()

d = 0

for line in lines:
    if "d" in line:
        d += 1

print(d)

Another error. If you want to count all occurrences and not just how many lines contain the letter, you’ll want to use str.count. Also you can avoid calling readline if you directly loop over the file:

d = 0
with open("file_x.txt", "r") as in_file:
    for line in in_file:
        d += line.count("d")

print(d)

Going a bit further with sum and a generator expression:

with open("file_x.txt", "r") as in_file:
    d = sum(line.count("d") for line in in_file)

print(d)