-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_names.py
More file actions
33 lines (24 loc) · 978 Bytes
/
Copy pathtest_names.py
File metadata and controls
33 lines (24 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Aufgabe:
#
# Im Ordner "data/names" gibt es einige Textdateien, die Namen von Personen enthalten (zufällig generiert).
#
# Schreibe ein Programm, welches alle Textdateien aus dem Ordner "names" einliest, und ermittelt, wie oft der Name
# "Max" insgesamt in allen Dateien vorkommt.
#
# Beispiel:
# Käme der Name "Max" in der Datei "1.txt" 1x vor, und in der Datei "2.txt" 2x, sonst aber nie, soll das Programm
# die Zahl 3 ausgeben.
import os
# setting the path for the data set
folder = os.path.join(os.path.dirname(__file__), "./data/names")
# counter for Max
max_count = 0
for file in os.listdir(folder):
name_file = os.path.join(folder, file)
# check ever line in every file for 'Max' and increase the counter if we found on
with open(name_file, "r", encoding="utf-8") as txtfile:
for line in txtfile:
person = line.strip().split(" ")
if person[0] == "Max":
max_count += 1
print(max_count)