English 中文(简体)
如何将python脚本添加到启动注册表?
原标题:How do I add a python script to the startup registry?

我试图让我的python脚本在启动时运行,但我收到错误消息windowserror访问被拒绝,但我应该能够让程序在启动时启动,因为每次我重新启动计算机时都会运行teamwiewer(我下载的第三方程序),所以我知道我应该能够在启动时运行我的程序(不过我可能正在做一些不同的事情,所以如果你能了解一下teamviewer的不同之处,让它的脚本在启动时执行,那将很有帮助)。

这是我的剧本

import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, SoftwareMicrosoftWindowsCurrentVersionRun )
_winreg.SetValueEx(key, pytest ,0,_winreg.REG_BINARY, C:Users"USERNAME"Desktop	est.py ) 
key.Close()
webbrowser.open( www.youtube.com )

欢迎提供任何意见。

最佳回答
import webbrowser
webbrowser.open( www.youtube.com )

把那些乱七八糟的东西都扔掉。相反,您(假设双击py文件会打开控制台)应该简单地将其放在启动文件夹中(在Windows 7上为C:UsersyourusernameAppDataRoamingMicrosoftWindows启动菜单程序启动,在XP中为C:Documents and SettingsyourusernameStart MenuProgramsStartup)。这是因为Windows尝试打开启动文件夹中的所有文件,如果Python默认打开PY,Windows将打开Python控制台。尝试重新启动,这应该有效。

问题回答

Baboon: I am a little late posting, but you seem to have left off the sam at the end of your code here.

当您打开一个密钥时,您需要添加用户权限,如果不添加,则_winreg默认为“READ”:

Here is a snippet from the python site http://docs.python.org/2/library/_winreg.html#access-rights

sam是一个整数,它指定一个访问掩码,用于描述密钥所需的安全访问。默认值为KEY_READ。有关其他允许的值,请参阅访问权限

以下是更正后的代码:

 import _winreg, webbrowser
    key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, SoftwareMicrosoftWindowsCurrentVersionRun ,_winreg.KEY_SET_VALUE)
    _winreg.SetValueEx(key, pytest ,0,_winreg.REG_BINARY, C:Users"USERNAME"Desktop	est.py ) 
    key.Close()
webbrowser.open( www.youtube.com )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...