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
#1
I need to write a Python program that iterates through all the data files in a folder and calculates the total number of occurrences of a given name. For example, the user would input a name, Bob, and the program has to open the folder that contains 100+ text files that contain a list of names and count every occurrence of the name 'Bob' and print the final result.

How do you work with folders in Python (I'm on a Mac)?
Reply
#2
https://docs.python.org/3/library/pathlib.html

Pathlib is a good place to start. Something like
import pathlib

path = pathlib.Path('./data_folder')
for entry in path.iterdir():
    if entry.is_file():
        print(entry)
Reply
#3
(Oct-29-2018, 09:09 PM)nilamo Wrote: https://docs.python.org/3/library/pathlib.html

Pathlib is a good place to start. Something like
import pathlib

path = pathlib.Path('./data_folder')
for entry in path.iterdir():
    if entry.is_file():
        print(entry)


This is a good start thanks. Now I need to search through all of the entries for a specified name the user inputs. If I was working with only one file I would do something like this
input = 'Enter a name: '
for line in f:
     if input in line:
          print(line)
How does it work with the multiple text files in the path?
Reply
#4
So you know how to open files, and go through them, and see all the files in the directory. So just connect the dots :p

import pathlib

path = pathlib.Path("./some_folder")
for entry in path.iterdir():
    if entry.is_file():
        with open(entry) as f:
            for line in f:
                if users_text in line:
                    print(line)
Reply
#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
#6
(Oct-30-2018, 05:57 PM)gonzo620 Wrote: 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?

okay I was able to get a little closer to my desired end result.
import pathlib

name = input('Enter a name: ')
name_list = []

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_list.append(name)
                    name_count = name_list.count(name)
                    print(name_count)
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
I just need the total number printed. I don't need the program to print all the counting
Reply
#7
import os

for _, _, files in os.walk(path):
    print(files) # files is a list of all the files in path
    print('total', len(path))
    break
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Oct-30-2018, 07:01 PM)gonzo620 Wrote: I just need the total number printed. I don't need the program to print all the counting
Then print after the loop, instead of inside it.
Reply
#9
Sorry for all the questions. For another assignment I now need to get the number of occurrences a given name has per file. The current program finds the total occurrence in all the files together. How can I get there from my current code?
Reply
#10
(Nov-06-2018, 12:50 AM)gonzo620 Wrote: from my current code
idk, what's your current code look like?
Reply


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,453 Dec-25-2021, 10:46 PM
Last Post: noahverner1995
  iterating through a number of files gonzo620 6 3,311 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