Python Forum

Full Version: iterating through a number of files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My current code will list the total number of occurrences of the user input within all the files in the path. How can I edit the code to show me the occurrence of the input for each txt file?

import pathlib
 
name = input('What name are you interested in? ')
sex = input('For what sex are you look for? ')

path = pathlib.Path("./Names") # adds all files in the 'Names' folder to the path
 
name_list = []
 
for entry in path.iterdir(): # loops through all the files
    if entry.is_file():
        with open(entry) as f: # will open each file with read permission
            for line in f: # loop for finding every occurence of 'name'
                if name in line:
                    if sex in line:
                        current = line.strip().split(',')
                        if current[0] == name:
                            name_list.append(name) # adds all lines with 'name' to a list
                            name_count = name_list.count(name) # counts entries in list
                     
print(name, 'has been used a total of', name_count, 'times')
Use a dictionary. And strip() current[0] if you aren't absolutely positive that the column will never have any leading or trailing spaces.

name_dict={}
...
    if entry.is_file():
        name_dict[entry]=0
...
                    if current[0].strip() == name:
                        name_list.append(name) # adds all lines with 'name' to a list
                        name_dict[entry] += 1  ## +1 for each name in this file
An alternative with generator functions
def iterfiles(path):
    return (entry for entry if path.iterdir() if entry.is_file())

def records(filename, name, sex):
    with open(filename) as file:
        for line in file:
            if (name in line) and (sex in line):
                yield line.strip().split(',')

for entry in iterfiles(path):
    s = sum(1 for r in records(entry, name, sex) if r[0].strip() == name)
    print(name, 'has been used', s, 'times in file', entry)
(Nov-21-2018, 11:21 PM)Gribouillis Wrote: [ -> ]An alternative with generator functions
def iterfiles(path):
    return (entry for entry if path.iterdir() if entry.is_file())

there a syntax error in this first def
My assignment is to find the number of times a given name shows up in per text file in the path of 100+ files (I should have a different number for each text file, not just 1 number for the total in the entire path)

This is what I currently have
import pathlib
  
name = input('What name are you interested in? ')
sex = input('For what sex are you look for? ')
 
path = pathlib.Path("./Names") # adds all files in the 'Names' folder to the path
  
name_list = []
  
for entry in path.iterdir(): # loops through all the files
    if entry.is_file():
        with open(entry) as f: # will open each file with read permission
            for line in f: # loop for finding every occurence of 'name'
                if name in line:
                    if sex in line:
                        current = line.strip().split(',')
                        if current[0] == name:
                            name_list.append(name) # adds all lines with 'name' to a list
                            name_count = name_list.count(name) # counts entries in list
                      
print(name, 'has been used a total of', name_count, 'times')
this code gives me the number of lines the name shows up in the path but as stated above I need this result for each of the text files, not just the total in the path. How can I edit this to get the desired result?
Sorry, the first if should be in ...
There's no need to double post. I removed the thread with no replies.