windows - Python 看不到 C :\Windows\System32\GroupPolicy 中的文件或文件夹

标签 windows python-2.7 redirect 32bit-64bit

我遇到了 python 无法查看计算机上确实存在的文件夹或文件的问题。我已经确保该路径中没有符号链接(symbolic link),并且我可以完全控制 NTFS 文件权限。我什至删除了所有隐藏属性。下面是我正在使用的脚本及其输出:

import os
path = 'C:\\Windows\\System32\\GroupPolicy\\Machine'
print path
test = os.path.exists(path)
print test

C:\Windows\System32\GroupPolicy\Machine
False

我不确定为什么在我确定文件夹确实存在时返回 False。如果我从路径中删除“\Machine”,它会返回 True。我已验证以下命令在命令提示符下有效:

if exist c:\Windows\System32\GroupPolicy\Machine echo found

任何有关如何在 python 中实现此功能的建议都将不胜感激。这是我使用的 python 版本: Win32 上的 Python 2.7.6(默认,2013 年 11 月 10 日,19:24:18)[MSC v.1500 32 位(英特尔)]

最佳答案

好吧,经过一番挖掘,发现它与权限无关,与文件系统重定向有关。由于我在 Windows x64 上使用 x86 版本的 python(使用 x86,因为我正在使用 py2exe),Windows 将 System32 和子目录上的任何查询重定向到 SysWOW64。这意味着我实际上查询的是不存在的“C:\Windows\SysWOW64\GroupPolicy\Machine”。

为了解决这个问题,我找到了如何使用此处找到的配方禁用文件系统重定向:http://code.activestate.com/recipes/578035-disable-file-system-redirector/

这是我的最终代码,现在可以禁用重定向并允许在 64 位计算机上打开和查询 System32 中的文件。

import ctypes
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)


disable_file_system_redirection().__enter__()
import os
path = 'C:\\Windows\\System32\\GroupPolicy\\Machine'
print path
test = os.path.exists(path)
print test

关于windows - Python 看不到 C :\Windows\System32\GroupPolicy 中的文件或文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949383/

相关文章:

linux - Windows Host Docker + WSL2 - 如何将 Windows 目录挂载到 Linux 容器

windows - Windows 上的 Memcached 无法忽略 SIGHUP : No error failed to daemon() in order to daemonize

windows - 使用自动热键捕获右键单击+左键单击;意想不到的行为

python - 使用 Python 将列添加到现有的 CSV 文件

Javascript - 页面重新加载脚本不起作用

windows - 单元测试和启动 MongoDb 服务器

python - 使用 Python 3.5 的 Rodeo 中的 "Unable to execute"消息

python - 解析和聚合 Google Analytics 浏览器版本 CSV 数据

c - 将输出从 stdout 重定向到字符串?

reactjs - 是否可以在 Next.js 中通过国际化重定向服务器端?