Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python os.listdir doesn't show all files

In my windows7 64bit system, there is a file named msconfig.exe in folder c:/windows/system32. Yes, it must exists.

But when i use os.listdir to search the folder c:/windows/system32, I didn't get the file. Here is the test code, in t1.py:

import os
files = os.listdir("c:/windows/system32")
for f in files:
    if f.lower() == "msconfig.exe":
        print(f)

After run python t1.py, I get nothing. Why the file missed? How can I list all files under a folder?

BTW: I am using python 3.3.0 32bit version under windows 7 64bit

like image 953
truease.com Avatar asked Apr 29 '13 04:04

truease.com


1 Answers

I don't think this is a Python-specific issue. Windows does interesting things with 32 bit processes when running a 64 bit OS. In this case, Windows is probably showing you the contents of C:\Windows\SysWOW64\ as system32 when running 32 bit python. SysWOW64 contains 32 bit versions of various Windows components for use with the 32 bit compatibility layer.

The following was run on a Windows 7 x64 system; explorer.exe (which in this case is 64 bit) definitely shows different contents for these folders, yet:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> 
>>> s32 = set(os.listdir('C:/Windows/System32'))
>>> s64 = set(os.listdir('C:/Windows/SysWOW64'))
>>> s32-s64 # the difference is an empty set!
set([])
like image 64
Colin Valliant Avatar answered Nov 17 '22 05:11

Colin Valliant