python os.listdir 不显示所有文件 [英] python os.listdir doesn't show all files

查看:114
本文介绍了python os.listdir 不显示所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 windows7 64 位系统中,c:/windows/system32 文件夹中有一个名为 msconfig.exe 的文件.是的,它必须存在.

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

但是当我使用os.listdir 搜索文件夹c:/windows/system32 时,我没有得到该文件.这是测试代码,在 t1.py 中:

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)

运行 python t1.py 后,我什么也没得到.为什么文件丢失了?如何列出文件夹下的所有文件?

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

顺便说一句:我在 windows 7 64bit 下使用 python 3.3.0 32bit 版本

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

推荐答案

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

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.

以下是在 Windows 7 x64 系统上运行的;explorer.exe(在本例中为 64 位)肯定会显示这些文件夹的不同内容,但是:

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

这篇关于python os.listdir 不显示所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆