2013-04-29 385 views
4

在我的windows7 64位系统中,文件夹c:/windows/system32中有一个名为msconfig.exe的文件。是的,它一定存在。python os.listdir不显示所有文件

但是,当我使用os.listdir来搜索文件夹c:/windows/system3 2,我没有得到该文件。下面是测试代码,在t1.py

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

运行蟒蛇t1.py后,我什么也没得到。 为什么文件错过了?如何列出文件夹下的所有文件?

BTW:我使用Windows 7 64位

+0

在Windows 7 ** 32位**的Python 3.2中,您的代码通常适用于我。你有没有试过搜索文件是否真的存在? – nhahtdh 2013-04-29 05:12:55

+0

也许你想'system64' – jamylak 2013-04-29 05:17:22

+0

@jamylak:'system32'是一个用词不当,但它实际上包含64位系统上的64位版本的DLL和EXE。 – nhahtdh 2013-04-29 05:18:14

回答

8

的python 3.3.0 32位版本,我不认为这是一个Python的特有问题。 Windows在运行64位操作系统时有有趣有32位进程的事情。在这种情况下,Windows可能会在运行32位python时向您显示C:\ Windows \ SysWOW64 \的内容为system32。 SysWOW64包含用​​于32位兼容层的各种Windows组件的32位版本。

以下是在Windows 7 x64系统上运行;的explorer.exe(在这种情况下是64位)绝对显示不同的内容为这些文件夹,但:尝试

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([]) 
+0

真棒评论!为我节省了很多时间。 – gies0r 2017-04-26 14:53:03

0

:的C:\Windows\System32代替c:/windows/system32

import os,sys 

files = os.listdir('C:\Windows\System32') 
for x in files: 
    if x == ('msconfig.exe'): 
     print(x) 
3

一个32位过程在64运行位Windows具有可用于此问题的sysnative别名。

 
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 
>>> 

参见File System Redirector (MSDN),它说:

32位应用程序可以通过替换%WINDIR%\ Sysnative为%WINDIR%\ System32下访问本地系统目录。