I want to access .txt files in 46 subdirectories and extract the number of 0s and 1s in the text of each file. So far I've written this code:
from pathlib import Path
def count_0s(paths):
for p in paths:
list_zeros = []
list_ones = []
for line in p.read_text().splitlines():
zeros = 0
zeros += line.count('0')
ones = 0
ones += line.count('1')
list_zeros.append(zeros)
list_ones.append(ones)
return list_zeros, list_ones
path = "/content/drive/MyDrive/data/classes/"
paths = Path(path).glob("*/marked*.txt")
n_zeros=count_0s(paths)
n_zeros
I want to get the function return in the form of 2 lists (one with the number of 0s and the other with the number of 1s) to use in a Pandas dataframe.
Sorry if the questions are duplicated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…