[Python] How to move all py files in one folder to another folder? How to write the contents of each line of a txt file into a list? Interpretation of system monitoring information about system processes and user processes

1. How to move all py files in one folder to another folder?

To move all .py files in one folder to another folder, we can use the command line or write a simple script to accomplish this task. Here are two methods:

1.1 Using the command line

  1. Open a command line terminal.
  2. Use the cd command to navigate to the source folder containing the .py file you want to move, for example:
cd 源文件夹的路径
  1. Use the mv command to move all .py files to the target folder, for example:
mv *.py 目标文件夹的路径

This will move all .py files in the source folder to the destination folder.

1.2 Using Python scripts

We can also write a Python script to accomplish this task. Here is an example script:

import os
import shutil

# 源文件夹和目标文件夹的路径
source_folder = '源文件夹的路径'
target_folder = '目标文件夹的路径'

# 获取源文件夹中所有的.py文件
py_files = [f for f in os.listdir(source_folder) if f.endswith('.py')]

# 移动.py文件到目标文件夹
for py_file in py_files:
    source_path = os.path.join(source_folder, py_file)
    target_path = os.path.join(target_folder, py_file)
    shutil.move(source_path, target_path)

print("所有.py文件已移动到目标文件夹。")

Replace the path to the source folder and the path to the destination folder in the above code with your actual folder paths and run the script. It will move all .py files in the source folder to the destination folder.

No matter which method we choose, make sure to back up important data before operating on files to prevent accidental deletion or movement of files.

2. How to write the contents of each line of a txt file into a list?

To write each line of a text file into a list, we can use the following Python code:

# 打开文本文件以读取每一行内容
file_path = "your_file.txt"  # 替换为您的文件路径
with open(file_path, "r") as file:
    lines = file.readlines()

# 移除每行末尾的换行符,并将其存储在列表中
content_list = [line.strip() for line in lines]

# 打印列表中的内容(可选)
for line in content_list:
    print(line)

Let’s take a look at the required txt file:

Insert image description here
We try:

# 打开文本文件以读取每一行内容
file_path = "D:\CodeProject\CLIP+SAM\converts\object365_dict.txt"
with open(file_path, "r") as file:
    lines = file.readlines()

# 移除每行末尾的换行符,并将其存储在列表中
content_list = [line.strip() for line in lines]

for line in content_list:
    print(line)

Insert image description here

If we want to remove the numbers and colons at the beginning of each line, we can use Python's string operations to process the text content. Here is sample code on how to do this:

# 打开文本文件以读取每一行内容
file_path = "D:\CodeProject\CLIP+SAM\converts\object365_dict.txt"
with open(file_path, "r") as file:
    lines = file.readlines()

# 移除每行末尾的换行符,并将其存储在列表中
content_list = [line.split(":")[1].strip() for line in lines]

for line in content_list:
    print(line)

Insert image description here

This code will split the content of each line in the text file into two parts, using colon as the separator, and then retain only the second part after splitting (index 1), that is, removing the numbers and colons. The final text content is stored in a list called content_list.

3. Interpretation of system monitoring information about system processes and user processes

When we connect to the server remotely, we need to monitor system information, as shown in the figure below:

Insert image description here

进程号 USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND
1607917 wangzhe+  20   0   14.2g   3.7g 805844 R 966.1   5.9 324:04.87 python
1600321 xurui     20   0   38.2g   3.0g 772920 R 114.3   4.8 277:11.00 python
 751772 root      20   0  840484  15288  11500 S   1.0   0.0  65:20.83 sunloginclient
1601054 xurui     20   0 3041568 120776  25076 S   0.7   0.2   1:44.64 wandb-service(2
    887 root     -51   0       0      0      0 S   0.3   0.0   6:54.00 irq/125-nvidia
    911 systemd+  20   0   14828   4104   3420 S   0.3   0.0   9:24.54 systemd-oomd
1566168 wangzhe+  20   0 1513096 651532  44308 S   0.3   1.0   4:10.73 node
1595962 wangzhe+  20   0  987216 169104  38516 S   0.3   0.3   1:34.90 node
1607462 xurui     20   0   38.3g   2.4g  86604 S   0.3   3.9   0:01.66 python
1610358 wangtao   20   0  956504  95180  45312 S   0.3   0.1   0:02.63 node
1610438 wangtao   20   0  991732 221932  39080 S   0.3   0.3   0:26.31 node
1611222 wangzhe+  20   0   22348   4572   3436 R   0.3   0.0   0:00.07 top

This is system monitoring information about system processes and user processes, usually obtained through top or similar tools. Here are some interpretations of this information:

  1. Process ID (PID): Each running process has a unique process ID, which is used to identify and manage the process in the system.
  2. USER: This is the user running the process.
  3. %CPU and %MEM: This is the CPU usage and memory usage of the process. %CPU represents the percentage of CPU currently being used by the process, and %MEM represents the percentage of memory currently being used by the process.
  4. COMMAND: This is the name of the command line command or program for the process.

Now let us focus on the information of several of these processes:

  1. The process number is 1607917, the user is wangzhe+, and the command is python. This process is using approximately 14.2GB of virtual memory and 3.7GB of physical memory. Its CPU usage is very high (%CPU is 966.1%), indicating that it is consuming a lot of CPU resources. Has been running for 324:04.87 hours.
  2. The process number is 1600321, the user is xurui, and the command is python. This process is using approximately 38.2GB of virtual memory and 3.0GB of physical memory. It also has relatively high CPU usage (%CPU of 114.3%) and has been running for 277:11.00 time.
  3. The process number is 751772, the user is root, and the command is sunloginclient. This process is using approximately 840MB of virtual memory and 15MB of physical memory. It has low CPU usage (%CPU is 1.0%) and has been running for 65:20.83 time.
  4. The process number is 1601054, the user is xurui, and the command is wandb-service. This process is using approximately 3GB of virtual memory and 120MB of physical memory. Already run 1:44.64 time.

The above information can help us understand the status and resource usage of running processes in the system for performance monitoring and troubleshooting. If we encounter performance issues or need to free up resources, we can use this information to take appropriate actions, such as optimizing code, killing unnecessary processes, or upgrading hardware.

How do you understand the time 324:04.87 above?

324:04.87 represents the running time of the process, usually expressed in the format of hours:minutes:seconds. In this example:

  • 324 means the process has been running for 324 hours.
  • 04 means the process has been running for 4 minutes.
  • 87 means that the process has been running for 87 seconds.

So, in total, the process with process number 1607917 has been running for 324 hours, 4 minutes, and 87 seconds. This is the total running time of the process and can be used to measure the stability and persistence of the process on the system.

Guess you like

Origin blog.csdn.net/wzk4869/article/details/132916016