0
for dirname, dirnames, filenames in os.walk("C:\\",followlinks=True,topdown=True):

    for subdirname in dirnames:    
        os.chdir(os.path.join(subdirname, dirname))

        if os.getcwd()!="C:\Windows\winsxs":
            print(os.getcwd())

As you can see, this code is supposed to search the entire C drive for all subdirectories and change Python's working directory and display the result. I can't help but notice for whatever reason, os.walk has been missing out quite a few subdirectories. It seems to find a big directory tree and then only scan about half of it before moving on (Or at least that's the case with the directories I've checked such as desktop)

I've scoured the net, but I can't seem to find anyone else who has had this problem, some help would be really appreciated (I am a novice coder).

EDIT: The solution worked, but now I get a permission error when I run the code.

2 Answers2

0

Your join is wrong. It must be:

os.chdir(os.path.join(dirname, subdirname))

That's why you never enter the highest directory levels.

Daniel
  • 42,087
  • 4
  • 55
  • 81
0

As documentation (here: https://docs.python.org/2/library/os.html#os.walk) states, to get a full path to a file or directory in dirpath, do os.path.join(dirpath, name).

Your join should be:

os.chdir(os.path.join(dirname, subdirname))

TO YOUR EDIT: If you want to search all directories, you may find some that you have no permission to enter or read as a normal user. If you want to do it, you'd probably have to use your administrator account.

Adalee
  • 528
  • 12
  • 26
  • That's odd because I am an Administrator. – matthiasguy1 May 01 '15 at 15:45
  • I don't use Windows, but I have found [this](http://stackoverflow.com/a/8988059): Even though your user is a member of the Administrators group, the program can't use Administrator privileges. When you select "Run as Administrator" and your user is an administrator the program is launched with the original unrestricted access token. Maybe it helps. – Adalee May 01 '15 at 16:32