Bí quyết kích hoạt tự động file Python khi cắm USB

Tiếng Việt

Trong bài viết này, chúng ta sẽ cùng nhau tìm hiểu cách kích hoạt tự động file Python khi cắm USB. Điều này có thể hữu ích cho nhiều mục đích khác nhau, chẳng hạn như:

Chạy một chương trình khi cắm USB để kiểm tra virus hoặc phần mềm độc hại.

Khởi chạy một trình cài đặt khi cắm USB để cài đặt phần mềm.

Tự động sao chép dữ liệu từ USB vào máy tính.

Cách thực hiện

Có hai cách để kích hoạt tự động file Python khi cắm USB:

Cách 1: Sử dụng tệp LOCK

Phương pháp này sử dụng một tệp LOCK để chỉ hiển thị khi thiết bị được cắm vào và không chạy nhiều phiên bản của code.

Tạo một tệp LOCK mới trong thư mục gốc của USB.

Mở tệp LOCK và thêm dòng sau:

Python

import time

 

def main():

while True:

time.sleep(1)

 

if __name__ == “__main__”:

main()

Use code with caution. Learn more

content_copy

Lưu tệp LOCK và đóng nó.

Cách 2: Sử dụng AutoRun.inf

Phương pháp này sử dụng tệp AutoRun.inf để tự động chạy file Python khi USB được cắm vào.

Tạo một tệp AutoRun.inf mới trong thư mục gốc của USB.

Mở tệp AutoRun.inf và thêm dòng sau:

[AutoRun]

Open=Python.exe

Lưu tệp AutoRun.inf và đóng nó.

Kết luận

Với hai cách trên, bạn có thể dễ dàng kích hoạt tự động file Python khi cắm USB. Phương pháp nào phù hợp với bạn sẽ phụ thuộc vào nhu cầu cụ thể của bạn.

English

In this article, we will learn how to activate Python file automatically when USB is plugged in. This can be useful for many purposes, such as:

Running a program when USB is plugged in to check for viruses or malware.

Starting an installer when USB is plugged in to install software.

Automatically copying data from USB to computer.

How to do it

There are two ways to activate Python file automatically when USB is plugged in:

Method 1: Using LOCK file

This method uses a LOCK file to only show when the device is plugged in and not run multiple versions of the code.

Create a new LOCK file in the root directory of the USB.

Open the LOCK file and add the following line:

Python

import time

 

def main():

while True:

time.sleep(1)

 

if __name__ == “__main__”:

main()

Use code with caution. Learn more

content_copy

Save the LOCK file and close it.

Method 2: Using AutoRun.inf

This method uses an AutoRun.inf file to automatically run the Python file when the USB is plugged in.

Create a new AutoRun.inf file in the root directory of the USB.

Open the AutoRun.inf file and add the following line:

[AutoRun]

Open=Python.exe

Save the AutoRun.inf file and close it.

Conclusion

With the two methods above, you can easily activate Python file automatically when USB is plugged in. The method that is right for you will depend on your specific needs.

Bí Quyết Kích Hoạt Tự Động: File Python Khi Cắm USB

Leave a Reply

All in one