Ran a loop that counted the number of rows of all .csv files in a folder. I now need to save these results to a dictionary.
import os
for filename in os.listdir(path):
with open(filename, 'r', encoding="latin-1") as fileObj:
if filename.endswith('csv'):
print('Rows counted {} in the csv {}'.format(len(fileObj.readlines()) -1, filename))
I get an output of what I want, but any way I try to save it to a dict results in "I/0 operation closed on file." (I am relatively new sorry if this is a dumb question)
Blockquote
Output:
Rows counted 840593 in the csv MA_2019_01.csv
Rows counted 818861 in the csv MA_2019_02.csv
Rows counted 914653 in the csv MA_2019_03.csv
Rows counted 1011374 in the csv MA_2019_04.csv
Rows counted 1088294 in the csv MA_2019_05.csv
Rows counted 1120696 in the csv MA_2019_06.csv
Rows counted 1119932 in the csv MA_2019_07.csv
Rows counted 1194284 in the csv MAg_2019_08.csv
Rows counted 1136953 in the csv MA_2019_09.csv
Rows counted 1010777 in the csv MA_2019_10.csv
Rows counted 901468 in the csv MA_2019_11.csv
Rows counted 950661 in the csv MA_2019_12.csv
CodePudding user response:
You can do this in a single dict comprehension :
import os
file_counts = {
filename: len(open(filename, "r").readlines()) - 1
for filename in os.listdir(path)
if filename.endswith(".csv")
}
CodePudding user response:
- Whether it is better to check the csv file before opening the file, why open unnecessary ones?
- The count starts from the first line, so there is no need to subtract one.It seems to me that it is more clear when not the length is calculated, but the sum of the lines.
- We add elements to the dictionary in a loop. I took the filename without the extension as the key.
import os
dict_count = dict()
for file in os.listdir(r'path'):
if file.endswith('csv'):
with open(file, 'r', encoding="latin-1") as f:
line_count = sum(1 for line in f.readlines())
file_name = os.path.splitext(file)[0]
dict_count[file_name] = line_count
print(dict_count)