python得到windows自启动列表的方法
时间:2020-11-07 22:07:53|栏目:Python代码|点击: 次
如下所示:
#!/usr/bin/python #-*-coding:utf-8-*- import _winreg as wr #导入内置的windows注册表操作库 import os from socket import * import getpass def GetStart(): key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run' aRegL = wr.ConnectRegistry(None,wr.HKEY_LOCAL_MACHINE) aRegC = wr.ConnectRegistry(None,wr.HKEY_CURRENT_USER)#getpass.getuser()为得到当前的用户名 startdir = os.listdir(r'C:\Users'+ os.sep + getpass.getuser() + r'\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup') startExe = {'localmachine':[],'currentuser':[]} startExe['startdir'] = startdir try: aKeyL = wr.OpenKey(aRegL,key)#打开键,返回句柄 aKeyC = wr.OpenKey(aRegC,key) for i in range(1024): try: startExe['localmachine'].append( wr.EnumValue(aKeyL,i)) except: break #异常就是终点,退出循环 for i in range(1024): try: startExe['currentuser'].append( wr.EnumValue(aKeyC,i)) except: break finally: wr.CloseKey(aKeyL) wr.CloseKey(aKeyC) return startExe def main(): import sys reload(sys) sys.setdefaultencoding( "utf-8" ) start = GetStart() #这个函数返回一个字典 print 'LocalMachine'.center(50,'*') for i in start['localmachine']:#迭代字典中的数据 print i print 'CurrentUser'.center(50,'*') for i in start['currentuser']: print i print 'StartUp dir'.center(50,'*') for i in start['startdir']: print i if __name__== "__main__": main()
栏 目:Python代码
下一篇:python:解析requests返回的response(json格式)说明
本文地址:http://www.codeinn.net/misctech/20166.html