Python子进程在Windows上删除reg键 [英] Python Subprocess to delete reg key on Windows

查看:152
本文介绍了Python子进程在Windows上删除reg键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码无法正常工作:

  import subprocess 

key ='reg删除HKEY_USERS\S-1-5-21-2637495828-1099793317-3825703309-1000\SOFTWARE\Classes\CLSID\ {0E270DAA-1BE6-48F2-AC49-D955BE4EEF1D} / f'
子进程。调用(key,shell = True)

运行Windows 10 64b,尝试使用subprocess.Popen aswell。 / p>

我的第一个想法是我必须逃避\不知何故,但我不知道这是我需要的,还是怎么做的。
我也试图使它与一个列表的参数 key = ['reg','delete','HKEY ...'])给出相同的结果



python脚本的结果

  C:\\ \\ test.py 
错误:找不到密钥或注册表值

来自Windows提示符的命令

  C:\reg delete HKEY_USERS\S-1-5-21-2637495828-1099793317 -3825703309-1000\SOFTWARE\Classes\CLSID\ {0E270DAA-1BE6-48F2-AC49-D955BE4EEF1D} / f 
操作成功

NB我导出的密钥,所以我可以在每次成功的尝试后再次添加。



N.B。 Windows返回消息被翻译,可能不准确。

解决方案

我以为这样( 32位部分)。您正在遇到由注册表重定向



换句话说(在64位 Windows )上,对于某些注册表项(包括这个) 2个独立位置:一个用于64位(默认和旧的),另一个用于32位(新位置)。默认情况下,64位应用程序会自动使用64位注册表位置;相同的东西32位(当然可以编程修改 - 至少对于64位应用程序)。



所以,为了使具有硬编码的一些注册表项的32位应用程序仍然可以在64位 Windows 上工作(记住在32位应用程序中硬编码的密钥现在是64位,并且它是 到应用程序,而 32位是其他地方), Microsoft 提出了这种方法。相同的方法适用于文件系统下的路径( System32 vs SysWOW64 在C:\Windows )。



现在为什么从cmdline工作?作为一个64位操作系统,默认的 cmd 是64位(启动64位 reg.exe ),所以它找到了密钥。是的,你有2个 cmd (每个上面的文件夹都有一个),实际上(几乎)所有的 Windows 可执行文件和dll是重复的。



要测试,请从 C:\Windows\SysWOW64 启动 cmd.exe ,然后运行 reg 命令,它将失败。



最后,要结束这个,关联 .py 文件(我注意到你直接运行它)与64位的版本(你可能需要下载并安装它)。


My code doesn't work as expected :

import subprocess

key = 'reg delete HKEY_USERS\S-1-5-21-2637495828-1099793317-3825703309-1000\SOFTWARE\Classes\CLSID\{0E270DAA-1BE6-48F2-AC49-D955BE4EEF1D} /f'
subprocess.call(key, shell=True)

Running Windows 10 64b, tried with subprocess.Popen aswell.

My first thought is I have to escape \ somehow, but I'm not sure if that's what I need, nor how to do it. I also tried to make it work with a list of args key = ['reg', 'delete', 'HKEY...']), which gave the same result

Result from python script

C:\test.py
Error : Couldn't find the key or registry value

Result from the command from the Windows prompt

C:\reg delete HKEY_USERS\S-1-5-21-2637495828-1099793317-3825703309-1000\SOFTWARE\Classes\CLSID\{0E270DAA-1BE6-48F2-AC49-D955BE4EEF1D} /f
Operation Suceeded

N.B. I exported the key so I can add it again after every successful try.

N.B. Windows return messages were translated, and may not be accurate.

解决方案

I thought so (the 32 bit part). You're running into a problem generated by Registry redirection.

In other words (on 64bit Windows), for some registry keys (this one included) there are 2 separate locations: one for 64bit(default and old one) and other for 32bit(a new location). By default a 64bit app automatically uses the 64bit registry location; same thing for 32 bit (of course that can be programmatically modified - at least for 64bit apps).

So, in order for 32bit apps that have some registry keys hardcoded to still work on 64bit Windows (remember the key hardcoded in the 32bit app is now 64 bit, and it's invisible to the app, while the real 32bit is someplace else), Microsoft came up with this approach. Same approach applies to paths on the filesystem (System32 vs SysWOW64 under C:\Windows).

Now why does it work from cmdline? Being a 64bit OS, the default cmd is 64 bit (which launches the 64bit reg.exe), so it finds the key. Yes you have 2 cmds (one under each of the above folders), actually (almost) all the Windows executables and dlls are duplicated.

To test, start cmd.exe from C:\Windows\SysWOW64, and run the reg command and it will fail.

Finally, to get past this, associate .py files (I noticed that you ran it directly) with a 64bit Python version (you might have to download and install it).

这篇关于Python子进程在Windows上删除reg键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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