0

I'm doing a udemy programming course and as part of that they had me write this code:

import os

listing = os.walk('.')
for root, directories, files in listing:
    print(root)
    for d in directories:
        print(d)
    for file in files:
        print(file)

since then, whenever I run any python program, even if it is this:

statement_1 = False
statement_2 = True

if statement_1 == True:
    print("Statement 1 is true")
elif statement_2 == True:
    print("Statement 2 is true")
else:
    print("both statements are false")

I get this error: C:\Users\Reinout\AppData\Local\Programs\Python\Python37-32\python.exe "C:/Users/Reinout/IdeaProjects/Learning projects/test.py" Fatal Python error: initsite: Failed to import the site module Traceback (most recent call last): File "C:\Users\Reinout\AppData\Local\Programs\Python\Python37-32\lib\site.py", line 73, in import os File "C:\Users\Reinout\IdeaProjects\Learning projects\functions**os**.py", line 3, in listing = os.walk('.') AttributeError: module 'os' has no attribute 'walk'

So the question is: why do i get errors for a different program when it doesn't even have anything to do with the program I'm running? I'm not trying to import the os module right now. and even if I was, it definitely does have the attribute 'walk'. I can click on it to see the documentation.

Dutch_597
  • 21
  • 3
  • Is one of your files called "os.py" by any chance? – dspencer Apr 06 '20 at 08:02
  • 2
    Probably because the second line of error indicates that OP does in fact have a file named `os.py` in `C:\Users\Reinout\IdeaProjects\Learning projects\functions`? – Edric Apr 06 '20 at 08:03
  • yes, I do. that's the file that contains the first bit of code. But even if that one is full of bugs I should still be able to run a second, unrelated program? the second bit of code is all of the code in test.py, there's no importing going on there. – Dutch_597 Apr 06 '20 at 08:09
  • Python needs to import a bunch of modules to support itself, and the stdlib `os` module is one of them. (`test` is another stdlib module, containing Python's own regression test suite and intended for internal use only, so that's another name you shouldn't use for your own code.) – user2357112 Apr 06 '20 at 08:12
  • oh! right. the problem was the name I gave the file, not the code in it. I deleted the file and now everything works fine again. thanks for the help! – Dutch_597 Apr 06 '20 at 08:25

0 Answers0