Python cannot see files or folders in C: \ Windows \ System32 \ GroupPolicy
I am having a problem where python cannot see folders or files that exist on the computer. I have already ensured that there are no symbolic links in the path and that I have full control over NTFS file permissions. I even removed all hidden attributes. Below is the script and its output:
import os
path = 'C:\\Windows\\System32\\GroupPolicy\\Machine'
print path
test = os.path.exists(path)
print test
C:\Windows\System32\GroupPolicy\Machine
False
I'm not sure why it returns False when I ensured that the folder exists. If I remove "\ Machine" from the path, it returns True. I checked the following command from the command line:
if exist c:\Windows\System32\GroupPolicy\Machine echo found
Any advice on how to get this working in python would be appreciated. Here is the python version I am using: Python 2.7.6 (default, Nov 10, 2013 7:24:18 pm) [MSC v.1500 32 bit (Intel)] on win32
source to share
Ok, after some digging, found that it has nothing to do with permissions, but has to do with file system redirection. Since I am using the x86 version of python for Windows x64 (using x86 as I am using py2exe), Windows will redirect any requests to System32 and subdirectories to SysWOW64. This means that I was actually asking for "C: \ Windows \ SysWOW64 \ GroupPolicy \ Machine" which did not exist.
To fix this, I discovered how to disable file system redirection using a recipe found here: http://code.activestate.com/recipes/578035-disable-file-system-redirector/
Here is my final code now working to turn off redirection and allow files to be opened and requested in System32 on a 64 bit machine.
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
source to share