插入 USB 设备时如何自动运行 Python 脚本? [英] How do I autorun my Python script when I plug in my USB device?

查看:57
本文介绍了插入 USB 设备时如何自动运行 Python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我的 USB 设备插入端口时,我都想执行我的 Python 脚本(在根目录下,autorun.pyw").我不想安装任何软件.我使用的是 Windows 10,并尝试使用 autorun.inf 文件.不胜感激.

I want to execute my Python script (at root, "autorun.pyw") whenever my USB device is plugged into a port. I don't want to install any software. I am using Windows 10, and I have tried using an autorun.inf file. Help would be greatly appreciated.

推荐答案

  1. 第一步:安装软件

恢复自动运行功能 要恢复自动运行"功能,我们首先需要下载 APO USB Autorun,这是一个免费的小程序,可以监控连接到计算机的 USB 设备.安装后,每当您连接 USB 存储设备(例如 U 盘)时,它都会检查设备内部是否有 autorun.inf 文件并运行配置的程序.

Restore the auto-run function To restore the "auto-run" function, the first thing we will need to do is download APO USB Autorun, a small free program that monitors the USB devices connected to the computer. Once installed, whenever you connect a USB storage device, such as a USB stick, it will check if there is an autorun.inf file inside the device and will run the configured program.

  1. 第二步:创建 autorun.inf

创建 autorun.inf 文件 要从闪存驱动器自动运行程序,它需要有两个东西,要自动运行的程序和指向闪存驱动器上程序的脚本文件.将要运行的程序的可执行文件复制到随身碟.然后打开记事本,复制下面的文本并将其粘贴到记事本窗口中以创建自动运行脚本.[自动运行];Open = MEUAPP.exe ShellExecute = MEUAPP.exe UseAutoPlay = 1 在记事本中粘贴文本后,将文本MEUAPP"替换为您复制到将自动运行的 U 盘的程序文件的名称,如图所示下面的例子.然后,将文件保存到名为 autorun.inf 的 U 盘.重要提示:确保在类型"字段中选择所有文件 (*.*)"选项,以便使用 INF 扩展名而不是 TXT 扩展名保存文件.闪存驱动器现在应该包含程序的可执行文件和您刚刚创建的 autorun.inf 文件.您可以使用 U 盘放置其他文件,但请务必保留这两个文件.注意:如果便携式程序有多个文件,您可以将其文件夹复制到 U 盘,只需确保在 ShellExecute 字段中输入路径即可.例如:ShellExecute = PASTADOAPP \ MEUAPP.exe.

Create the autorun.inf file To automatically run a program from the flash drive, it needs to have two things, the program you want to run automatically and a script file that points to the program on the flash drive. Copy the executable file of the program you want to run to the pendrive. Then open the notepad, copy the text below and paste it into the notepad window to create the autorun script. [autorun]; Open = MEUAPP.exe ShellExecute = MEUAPP.exe UseAutoPlay = 1 Once you have pasted the text in the notepad, replace the text "MEUAPP" with the name of the program file you copied to the USB stick that will run automatically, as shown in the example below. Then, save the file to the USB stick named autorun.inf. Important: Make sure to select the option "All files (*. *)" In the "Type" field so that the file is saved with an INF extension and not a TXT extension. The flash drive should now contain the program's executable file and the autorun.inf file you just created. You can use the USB stick to place other files, but be sure to keep both files. Note: If the portable program has multiple files, you can copy its folder to the USB stick, just make sure to enter the path in the ShellExecute field. Ex: ShellExecute = PASTADOAPP \ MEUAPP.exe.

  1. 最后一步:配置并自动运行

自动运行程序:完成此操作后,每当您将随身碟连接到计算机时,A​​PO USB Autorun 都会检测随身碟内的 autorun.inf 文件并自动运行您配置的程序.最后,请注意,您需要在要使用自动运行的所有计算机上安装 APO USB 自动运行.不过,如果您在同一台计算机上工作,它仍然很有用.

Run the program automatically: Once this is done, whenever you connect the pendrive to the computer, APO USB Autorun will detect your autorun.inf file inside the pendrive and will automatically run the program you have configured. Finally, note that you will need to install APO USB Autorun on all computers where you want to use autorun. Still, it can still be useful if you work on the same computers.

这篇关于插入 USB 设备时如何自动运行 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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