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