Python Forum
iterating through a number of files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating through a number of files
#1
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')
Reply
#2
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
Reply
#3
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)
Reply
#4
(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
Reply
#5
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?
Reply
#6
Sorry, the first if should be in ...
Reply
#7
There's no need to double post. I removed the thread with no replies.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  iterating through all files from a folder gonzo620 10 8,168 Nov-06-2018, 01:48 AM
Last Post: gonzo620

Forum Jump:

User Panel Messages

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