Your file[-3:] == ".py"
check is later that it should be. It is part of the check executed for every line of a file, instead of only being done once per file. You should use:
for file in os.listdir(working_directory):
if file[-3:] != ".py":
...
Are there other files in the directory? Maybe a README
, Makefile.mak
or .gitignore
? Maybe you want to only examine .h
files, and/or .hpp
files, instead of every file in the directory?
valid_exts = { ".h", ".hpp"}
for file in os.listdir(working_directory):
if os.path.splitext(file)[1] in valid_exts:
...
Using {line_number + 1}
in your format string is not very pretty. Line numbers start at one, and enumerate()
allows you to specify the starting number:
for line_number, line in enumerate(source_file, 1):
Why accumulate the results in occurrences
, and then write them out afterwords? Why not write them out as they are found?
with open("logs/log.txt", "w") as log_file:
for file in os.listdir(working_directory):
...