Python 实现Windows开机运行某软件的方法 | 您所在的位置:网站首页 › Python打开qq软件 › Python 实现Windows开机运行某软件的方法 |
Python 实现Windows开机运行某软件的方法
背景
很多时候我们需要在Windows操作系统中开机自动运行某个软件,例如开机自动运行QQ,自动运行Chrome等。本文将使用Python来实现这个功能。 实现过程 第一步:制作VBS脚本首先我们需要制作一个VBS脚本,以实现在Windows开机时自动启动某个应用程序的目的。具体的代码如下: Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\app.exe" Set WshShell = Nothing代码中的C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\app.exe是软件的路径,需要修改成你要自启动的软件路径。 将上述代码保存为一个.vbs文件,例如startApp.vbs。 第二步:写Python脚本实现开机自动运行接下来我们需要写一个Python脚本,以实现在Windows开机时自动运行我们刚才制作的VBS脚本。具体的代码如下: import os start_vbs = r"C:\Users\Administrator\startApp.vbs" # VBS脚本路径,需要修改 start_vbs_cmd = 'cmd /c "{}"'.format(start_vbs) # VBS脚本转换成CMD命令 key = r'Software\Microsoft\Windows\CurrentVersion\Run' reg_cmd = 'reg add "HKCU\{}" /v start_app /t reg_sz /d "{}" /f'.format(key, start_vbs_cmd) # CMD注册表命令 os.system(reg_cmd)代码中的start_vbs变量表示刚才制作的VBS脚本的路径,需要修改成你刚才保存的脚本路径。代码中的key变量表示启动项的位置,一般情况下为Software\Microsoft\Windows\CurrentVersion\Run。 第三步:运行Python脚本将上述代码保存为一个.py文件,例如auto_run.py,双击运行Python文件即可在系统注册表中添加自动启动项。 示例说明 示例一在实现开机自动启动QQ的例子中,制作的VBS脚本如下: Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "C:\Program Files (x86)\Tencent\QQ\Bin\QQ.exe" Set WshShell = Nothing相应的Python程序内容如下: import os qq_vbs_path = r"C:\Users\Administrator\Desktop\startQQ.vbs" start_vbs_cmd = 'cmd /c "{}"'.format(qq_vbs_path) key = r'Software\Microsoft\Windows\CurrentVersion\Run' reg_cmd = 'reg add "HKCU\{}" /v start_QQ /t reg_sz /d "{}" /f'.format(key, start_vbs_cmd) os.system(reg_cmd)将VBS脚本修改好,保存到指定路径,并修改Python程序中的VBS脚本路径和自动启动项名字,运行Python程序即可实现开机自动启动QQ。 示例二在实现开机自动启动Chrome的例子中,制作的VBS脚本如下: Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" Set WshShell = Nothing相应的Python程序内容如下: import os chrome_vbs_path = r"C:\Users\Administrator\Desktop\startChrome.vbs" start_vbs_cmd = 'cmd /c "{}"'.format(chrome_vbs_path) key = r'Software\Microsoft\Windows\CurrentVersion\Run' reg_cmd = 'reg add "HKCU\{}" /v start_chrome /t reg_sz /d "{}" /f'.format(key, start_vbs_cmd) os.system(reg_cmd)将VBS脚本修改好,保存到指定路径,并修改Python程序中的VBS脚本路径和自动启动项名字,运行Python程序即可实现开机自动启动Chrome。 本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python 实现Windows开机运行某软件的方法 - Python技术站 |
CopyRight 2018-2019 实验室设备网 版权所有 |