簡體   English   中英

python os.listdir不顯示所有文件

[英]python os.listdir doesn't show all files

在我的windows7 64位系統中,文件夾c:/windows/system32有一個名為msconfig.exe的文件。 是的,它必須存在。

但是當我使用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)

運行python t1.py ,我什么都沒得到。 為什么錯過文件? 如何列出文件夾下的所有文件?

BTW:我在windows 7 64bit下使用python 3.3.0 32bit版本

我不認為這是特定於Python的問題。 在運行64位操作系統時,Windows使用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([])

在64位Windows上運行的32位進程具有可用於此問題的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
>>>

請參閱文件系統重定向器(MSDN) ,其中說:

32位應用程序可以通過%windir%\\ Sysnative替換%windir%\\ System32來訪問本機系統目錄。

嘗試: 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM