如何在Python中向注册表添加新值? [英] How Do I Add A New Value To Registry In Python?

查看:495
本文介绍了如何在Python中向注册表添加新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

 REG ADD HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \RunOnce / t REG_SZ / v test / d C:\ Users从CMD添加值\ xxxxx \Desktop\xxx.exe 



这完美无缺,但是当我尝试用python做同样的事情

 os.system(  REG ADD HKEY_LOCAL_MACHINE \\ SOFTWARE \ Microsoft \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\RunOnce / t REG_SZ / v test / d C:\\Users\\xxxx \\Desktop\\xxx.exe



它返回0,根据CMD REG /?意味着它是成功的,但在RunOnce中没有名为test的值,我尝试使用

 subprocess.call([ ])

但它给出了相同的结果,我也以管理员身份执行了所有脚本。

解决方案

以下链接显示默认返回值为0.如果您添加了更多代码,有人可能会帮助您更多但是......由于给出的信息有限,我只能说是的,你得到0但这是默认的返回值。





https: //docs.python.org/2/library/os.html



os.system(命令)

执行子shell中的命令(字符串)。这是通过调用标准C函数系统()来实现的,并且具有相同的限制。对sys.stdin等的更改不会反映在已执行命令的环境中。



在Unix上,返回值是编码的进程的退出状态为wait()指定的格式。请注意,POSIX没有指定C system()函数返回值的含义,因此Python函数的返回值取决于系统。



< b>在Windows上,返回值是系统shell在运行命令后返回的值,由Windows环境变量COMSPEC给出:在command.com系统上(Windows 95,98和ME),它始终为0;在cmd.exe系统(Windows NT,2000和XP)上,这是命令运行的退出状态;在使用非本机shell的系统上,请参阅shell文档。



子进程模块提供更强大的工具来生成新进程并检索其结果;使用该模块比使用此功能更可取。有关一些有用的配方,请参阅子流程文档中的子流程模块部分替换旧功能。



可用性:Unix,Windows。

< ; /预>

I tried adding a value from CMD with

REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /t REG_SZ /v test /d C:\Users\xxxx\Desktop\xxx.exe


And that worked perfectly, but when i tried doing the same thing in python with

os.system("REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\Microsoft\\Windows\\CurrentVersion\\RunOnce /t REG_SZ /v test /d C:\\Users\\xxxx\\Desktop\\xxx.exe")


It returned 0, which according to CMD REG /? Means it's succesfull, but there is no value named "test" in RunOnce, i tried the same thing with

subprocess.call([""])

But it gave the same result, also i executed all scripts as an Administrator.

解决方案

The Following link shows that the default return value is 0. If you added some more code somebody would probably be able to help you more but... With the limited info given , I can only state that yeah, you're getting "0" but thats the default return value.


https://docs.python.org/2/library/os.html

os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

Availability: Unix, Windows.
</pre>


这篇关于如何在Python中向注册表添加新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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