Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python os.listdir不';不显示所有文件_Python_Python 3.3 - Fatal编程技术网

python os.listdir不';不显示所有文件

python os.listdir不';不显示所有文件,python,python-3.3,Python,Python 3.3,在我的windows7 64位系统中,文件夹c:/windows/system32中有一个名为msconfig.exe的文件。是的,它一定存在 但是当我使用os.listdir搜索文件夹c:/windows/system32时,我没有得到该文件。以下是测试代码,位于t1.py: import os files = os.listdir("c:/windows/system32") for f in files: if f.lower() == "msconfig.exe":

在我的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之后,我什么也没有得到。
为什么文件漏了?如何列出文件夹下的所有文件


顺便说一句:我在Windows7下使用的是python 3.3.0 32位版本,64位

我不认为这是python特有的问题。Windows在运行64位操作系统时使用32位进程做了一些有趣的事情。在本例中,在运行32位python时,Windows可能会将C:\Windows\SysWOW64\的内容显示为system32。SysWOW64包含各种Windows组件的32位版本,用于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([])

尝试:
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)

在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 >>> C:\Windows\System32>systeminfo |查找“系统类型” 系统类型:基于x64的PC C:\Windows\System32>dir/b msconfig.exe msconfig.exe C:\Windows\System32>python win32上的Python 2.7.6(默认,2013年11月10日19:24:18)[MSC v.1500 32位(英特尔)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。 >>>导入操作系统 >>>os.listdir中的'msconfig.exe'(r'c:\windows\system32') 假的 >>>os.listdir中的'msconfig.exe'(r'c:\windows\sysnative') 真的 >>> 看,上面写着:

32位应用程序可以通过将%windir%\Sysnative替换为%windir%\System32来访问本机系统目录


在32位Windows 7中的Python 3.2上,您的代码对我来说正常工作。你试过搜索文件是否确实存在吗?也许你想要
system64
@jamylak:
system32
用词不当,但它实际上包含64位系统上的64位版本的dll和exe。你对
os.access('c:/windows/system32/msconfig.exe',os.R\u OK)
,,另外,为了检查我的机器是否区分大小写,这里有“C:\WINDOWS\system32\dllcache\msconfig.exe”很棒的注释!节省了我很多时间。