Python Forum
iterating through all files from a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating through all files from a folder
#5
You've been a huge help! I just have one last question.
import pathlib

name = input('Enter a name: ')

path = pathlib.Path("./Names")
for entry in path.iterdir():
    if entry.is_file():
        with open(entry,'r') as f:
            for line in f:
                if name in line:
                    name_count = line.count(name)
                    print(name_count)
this program is printing a 1 for every occurrence of 'name'. What I want is to print the total number of occurrences 'name' has throughout all the files. What am I missing?
Reply


Messages In This Thread
RE: iterating through all files from a folder - by gonzo620 - Oct-30-2018, 05:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How can I create a linked list that contains each folder with its files? noahverner1995 8 2,652 Dec-25-2021, 10:46 PM
Last Post: noahverner1995
  iterating through a number of files gonzo620 6 3,453 Nov-26-2018, 10:02 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020