Python Forum

Full Version: iterating through all files from a folder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Nov-06-2018, 01:29 AM)nilamo Wrote: [ -> ]
(Nov-06-2018, 12:50 AM)gonzo620 Wrote: [ -> ]from my current code
idk, what's your current code look like?

import pathlib

name = input('What name are you interested in? ')
sex = input('Are you looking for M or F with that name? ')

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:
                    current = line.strip().split(',')
                    if sex in line:
                        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 for', sex)
similar to the posts above
Pages: 1 2