0

I tried to use the following example but it is not working when I'm adding a new file to the directory:

linecount = 0
path = input()

for root, dirs, files in os.walk(path):
    for file in files:
        if file.endswith('.py'):
            with open(file, 'r') as file:
                for line in file:
                    linecount += 1
Booboo
  • 38,656
  • 3
  • 37
  • 60
loukek
  • 1
  • 4
    Oh please do tell what's not working! It's a little difficult to understand based on your description. – Booboo Apr 30 '20 at 18:58
  • So basically I need the code to count all the lines in only the .py files with a path that I give in the input. At first I thought it was working when I gave the input this path: 'D:/python/files'. In that folder, there was one .py file which had 54 lines, and that was the same as the result i got from linecount at the end. But when I added a new file to that folder which was also a .py file and had 5 lines, I get an error: [Errno 2] No such file or directory: 'filename.py' – loukek Apr 30 '20 at 19:07
  • Don't forget you need to combine 'root' with 'file' to get the filename relative to the current working directory (see https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – bgstech Apr 30 '20 at 19:08

0 Answers0