7

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

jamylak
  • 128,818
  • 30
  • 231
  • 230
truease.com
  • 1,261
  • 2
  • 17
  • 30
  • On Python 3.2 in Windows 7 **32-bit**, your code works normally for me. Have you tried searching whether the file is actually there or not? – nhahtdh Apr 29 '13 at 05:12
  • Maybe you want `system64` – jamylak Apr 29 '13 at 05:17
  • @jamylak: `system32` is a misnomer, but it actually contains 64-bit version of the dll and exe on 64-bit system. – nhahtdh Apr 29 '13 at 05:18
  • What do you have for `os.access('c:/windows/system32/msconfig.exe', os.R_OK)`, also for check case sensitivity – wim Apr 29 '13 at 05:22
  • On my machine it is here "C:\WINDOWS\system32\dllcache\msconfig.exe" – Marichyasana Apr 29 '13 at 05:32
  • @truease.com where are you running this? IDLE? – jamylak Apr 29 '13 at 05:46
  • in ipython os.access("c:/windows/system32/msconfig.exe", os.R_OK) get False, os.path.isfile("c:/windows/system32/msconfig.exe") get False. in cmd.exe, dir c:\windows\system32\msconfig.exe get the file's information. – truease.com May 03 '13 at 03:13
  • i tried to find it in cygwin, and it also failed. "cd /cygdrive/c/Windows/System32", and then "ls | grep -i msconfig " gets nothing – truease.com May 03 '13 at 03:22

3 Answers3

9

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([])
Colin Valliant
  • 1,899
  • 1
  • 13
  • 20
6

A 32-bit process running on 64-bit Windows has the sysnative alias available for this problem.

C:\Windows\System32>systeminfo | find "System Type"
System Type:               x64-based PC

C:\Windows\System32>dir /b msconfig.exe
msconfig.exe

C:\Windows\System32>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> 'msconfig.exe' in os.listdir(r'c:\windows\system32')
False
>>> 'msconfig.exe' in os.listdir(r'c:\windows\sysnative')
True
>>>

See File System Redirector (MSDN), which says:

32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32.

azhrei
  • 2,303
  • 16
  • 18
0

try: C:\Windows\System32 instead of c:/windows/system32

import os,sys

files = os.listdir('C:\Windows\System32')
for x in files:
    if x == ('msconfig.exe'):
        print(x)
pmod
  • 10,450
  • 1
  • 37
  • 50