How do I iterate over files in a directory including sub directories, using Python?

Question:

I try writing a script that counts the lines of code in a project.

The problem I have is it doesn’t find all files.

The script looks like this:

import os

root = r"C:Usersusernamedataprojectstest"

allLines = []

for path, subdirs, files in os.walk(root):
    for name in files:
        filepath = os.path.join(path, name)

        if not filepath.endswith( ('.cs','.vb') ):
            break

        with open(filepath) as f:
            lines = f.read().splitlines()

            for line in lines:
                allLines.append(line)

print(len(allLines))

What’s wrong with the code?

Asked By: stax76

||

Answers:

In your case the issue is the break, if the file doesn’t end with .cs or .vb you just skip the directory, you need to change it for continue as follows:

import os

root = r"C:UsersfrankDatenProjekteCSmpv.net"

allLines = []

for path, subdirs, files in os.walk(root):
    for name in files:
        filepath = os.path.join(path, name)

        if not filepath.endswith( ('.cs','.vb') ):
            continue

        with open(filepath) as f:
            lines = f.read().splitlines()

            for line in lines:
                allLines.append(line)

print(len(allLines))

This code can also receive improvements:

import os

root = r"C:UsersfrankDatenProjekteCSmpv.net"

allLines = 0

for path, subdirs, files in os.walk(root):

    for name in files:

        if not filepath.endswith( ('.cs','.vb') ):
            continue

        filepath = os.path.join(path, name)

        with open(filepath) as f:
            lines += len(f.read().splitlines())

print(allLines)
Answered By: Shailyn Ortiz