win7下python禁止开机自启动

来源:互联网 发布:二维码打印机软件 编辑:程序博客网 时间:2024/04/28 11:06

win7下python禁止开机自启动


      今日学习了下python,正好最近正烦一些视频播放器一打开就将启动项加入到注册表,导致开机巨卡。所以参考网上资料,写了个python脚本,删除这些恼人的开机自启动项。

      主要的想法就是引入win32api和wincon,操作注册表,要安装python相应版本的winall包http://starship.python.net/crew/mhammond/downloads/
,我安装的python2.6.6,将HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run下的自启动项放入disableList.ini里,比如:

#config of disable items that not auto run at windows startup.#check the auto run items under regedit.exe of HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run#and add them under the list, save and exit, we will disable them auto run.Funshion   QyKernel  ThunderBaofengPlatform

       然后python读取配置文件,删除对应的注册表项,代码如下:

</pre><pre name="code" class="python">#filename:disableAutoRun.py#disable all things that auto run at the windows startup.import win32apiimport win32con#read config file of disable auto run app list.cnfList = []def readCnf(filename):    fp = open(filename, 'r')    for disItem in fp.readlines():                            if not len(disItem) or disItem.startswith('#') or disItem.startswith('\n'):            continue                    # delete the space ' ' at the begin or end of the string            disItem = disItem.strip()                    #print '', disItem        cnfList.append(disItem)    fp.close()    print "disable auto run list:\n", cnfList      #find disableItem under regedit, delete all in the config files.def delAllDisableKeys(disableItem, subKey, valueName):    global key    try:           key = win32api.RegOpenKey(disableItem, subKey, 0, win32con.KEY_ALL_ACCESS)                #print key, value        #value = win32api.RegQueryValueEx(key, valueName)        #print key, value                                #now delete all valueName that we don't want startup.        win32api.RegDeleteValue(key, valueName)        print 'successfully disable the ', valueName, 'auto run item.'    except IOError:        print 'io error.'    except IndentationError:        print 'IndentationError error.'    except:        print 'fail to disable the ', valueName, ', maybe it not auto run.'    finally:        #print 'clost the item:', valueName                    #close the key.        win32api.RegCloseKey(key)    def pause():    print '\n\npress any to exit...'    temps = raw_input()    #do the regedit that disable them.#check app in the dir.currentUserKey='Software\Microsoft\Windows\CurrentVersion\Run'#check app in the dir.currentLocalKey='Software\Microsoft\Windows\CurrentVersion\Run'#main()if __name__ == '__main__':    readCnf('.\\disableList.ini')        print 'now disable the auto run items:'    print '\n\ndisable auto run items under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run:'    for valueName in cnfList:        delAllDisableKeys(win32con.HKEY_CURRENT_USER, currentUserKey, valueName)    print '\n\ndisable auto run items under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run:'    for valueName in cnfList:        delAllDisableKeys(win32con.HKEY_LOCAL_MACHINE, currentLocalKey, valueName)    pause()



需要使用管理员运行,不然HKEY_LOCAL_MACHINE下的项不能打开。


参考:http://www.cnblogs.com/xiaowuyi/articles/2433161.html



0 0