2013-04-29 388 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下訪問本地系統目錄。