Retrieve .py under the qpython folder, copy the file to a separate folder and give a progress bar

basic tasks

检索qpython文件夹下的.py,将文件复制单独文件夹并给出进度条

Detailed description

  1. First, the os and shutil modules are imported, which are used to perform file and folder operations and copy file operations respectively.

  2. Then the source folder path and destination folder path are defined. The source folder path specifies the folder that needs to be traversed, while the destination folder path specifies the destination path for the copied files.

  3. Then use the os module to create the target folder. If the target folder does not exist, it is created through the os.makedirs function.

  4. Use the os.listdir function to traverse all file names in the source folder, use a for loop to process each file, and determine whether the file ends with .py. If so, perform the next step.

  5. Generate new file name. In the target folder, the file name consists of a serial number and the file name. The serial number indicates the order of the file in the target folder. The value of the serial number is equal to the number of existing files in the target folder plus 1. For example, if there is already a file in the target folder, then the serial number of the current file is 2, and so on.

  6. Build source file path and target file path. Use the os.path.join function to concatenate the file name and folder path to obtain the source file path src_file_path and the target file path dst_file_path.

Insert image description here

  1. Copy the files to the destination folder. Use the shutil.copy function to copy the source files into the destination folder.

  2. In the above operation, the file copying process is time-consuming. If there are many files, the user will have to wait longer. Therefore, we can use the tqdm library to add a progress bar function to allow users to understand the copy progress more intuitively.

  3. First, we use the from tqdm import tqdm statement to import the tqdm function from the tqdm library.

  4. Next, get py_files, a list of all .py files in the source folder. This can be achieved through a list comprehension, iterating through all the file names in the source folder and adding the file names ending in .py to the py_files list.

  5. Use the tqdm function to create a progress bar. The description information of the progress bar is 'Copying files', which can also be modified as needed. Use a for loop to traverse the py_files list and process each file.

  6. The operation in the for loop is similar to the previous code, using steps such as generating a new file name, constructing the source file path and target file path, and copying the file to the target folder.

  7. Outside the for loop, when you now run the program, you will see a process of copying files with a progress bar. The progress bar displays the progress of copying files, allowing users to understand the copying process more intuitively.

code

Add a simple progress bar. You can use the tqdm library to implement the progress bar function. First, make sure you have installed the tqdm library. If not, you can use the following command to install it:

pip install tqdm

Then, you can use the following code to implement the progress bar functionality:

import os
import shutil
from tqdm import tqdm

# 源文件夹路径
src_folder = '/storage/emulated/0/qpython'
# 目标文件夹路径
dst_folder = '/storage/emulated/0/qpython/py文件'

# 创建目标文件夹
if not os.path.exists(dst_folder):
    os.makedirs(dst_folder)

# 获取源文件夹中的.py文件列表
py_files = [file_name for file_name in os.listdir(src_folder) if file_name.endswith('.py')]

# 使用tqdm创建进度条,并遍历.py文件列表
for file_name in tqdm(py_files, desc='Copying files'):
    # 生成新文件名
    new_file_name = f"{
      
      len(os.listdir(dst_folder)) + 1}_{
      
      file_name}"
    # 构建源文件路径和目标文件路径
    src_file_path = os.path.join(src_folder, file_name)
    dst_file_path = os.path.join(dst_folder, new_file_name)
    # 复制文件到目标文件夹
    shutil.copy(src_file_path, dst_file_path)

In the above code, we create a progress bar by passing a list of .py files to the tqdm function.
Insert image description here
Insert image description here
A progress bar shows the progress of copying files. You can modify the description information of the progress bar as needed. Run the above code and you will see a file copying process with progress.

Supongo que te gusta

Origin blog.csdn.net/weixin_73675558/article/details/133498745
Recomendado
Clasificación