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
#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


Messages In This Thread
iterating through a number of files - by gonzo620 - Nov-21-2018, 09:56 PM
RE: iterating through a number of files - by woooee - Nov-21-2018, 10:30 PM
RE: iterating through a number of files - by Gribouillis - Nov-21-2018, 11:21 PM
RE: iterating through a number of files - by nilamo - Nov-26-2018, 10:02 PM
iterating through a path - by gonzo620 - Nov-26-2018, 09:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  iterating through all files from a folder gonzo620 10 8,359 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