stax76
stax76

Reputation: 520

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

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:\Users\username\data\projects\test"

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?

Upvotes: 0

Views: 145

Answers (1)

Shailyn Ortiz
Shailyn Ortiz

Reputation: 766

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:\Users\frank\Daten\Projekte\CS\mpv.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:\Users\frank\Daten\Projekte\CS\mpv.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)

Upvotes: 2

Related Questions