import subprocess import os import ctypes content = '''Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Office\\16.0\WEF\TrustedCatalogs\{83b67042-8bdb-4f94-a9e5-73466fe31176}] "Id"="{83b67042-8bdb-4f94-a9e5-73466fe31176} " "Url"=\\tsgscvmm02\Library01\Executables\IntelAgree "Flags"=dword:00000001 ''' 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) FilePath = os.path.expanduser('~') + r'\Desktop\TrustNetworkShareCatalog.reg' with open(FilePath, 'w') as w: w.write(content + "\n") print "file written successfully in : " + FilePath if os.path.exists(FilePath): try: with disable_file_system_redirection(): obj= subprocess.Popen('reg import '+ FilePath, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) out, err = obj.communicate() ret = obj.returncode if ret == 0: if out: print out.strip() else: print ret else: if err: print err.strip() else: print ret except: print "Registry Update failed" os.remove(FilePath)