python(10): modify file names in batches

1. Use Python's string operations to remove extra zeros from the high bits of the filename

illustrate

The file name in a directory is in the format of a 6-digit serial number plus a suffix, for example 000001.txt, 000002.jpg, 000003.csv, and you can use Python’s string operations to remove high-order extra zeros.

the code

Here is a sample code:

import os

directory = '/path/to/directory'  # 替换为你的目录路径

# 获取目录下所有文件名
filenames = os.listdir(directory)

# 遍历文件名并去除高位多余的零
for filename in filenames:
    basename, extension = os.path.splitext(filename)
    new_filename = basename.lstrip('0') + extension
    old_path = os.path.join(directory, filename)
    new_path = os.path.join(directory, new_filename)
    os.rename(old_path, new_path)

In the above code, we first use os.listdirthe function to get all the file names under the directory. Then, do the processing for each filename. With os.path.splitextthe function , we split the filename into basename and extension. Next, use lstripthe function to strip off excess zeros in front of the basename and reassemble it with the extension to form a new filename. Finally, use os.renamethe function to modify the original filename to the new filename.

Note: Before using the code, make sure to back up the files in the directory to avoid data loss due to accidental modification.

example

before operation
insert image description here

after operation
insert image description here

2. Rename the files in the folder to an incremented number plus a suffix of the original filename

Use modules in Python osto iterate over files in a folder and use os.renamefunctions to do file renaming. Here's a sample code that shows how to rename files in a folder to an incremented number plus a suffix of the original filename:

the code

import os

folder_path = 'path/to/folder'  # 文件夹路径

file_list = os.listdir(folder_path)  # 获取文件夹中的文件列表
file_list.sort()  # 按照文件名排序

for i, file_name in enumerate(file_list, start=1):
    file_extension = os.path.splitext(file_name)[1]  # 获取文件名后缀
    new_file_name = f"{
      
      i}{
      
      file_extension}"  # 新的文件名
    
    old_file_path = os.path.join(folder_path, file_name)  # 原始文件路径
    new_file_path = os.path.join(folder_path, new_file_name)  # 新的文件路径
    
    os.rename(old_file_path, new_file_path)  # 执行文件重命名操作

In the above code, needs to be 'path/to/folder'replaced with the actual path of the folder to be renamed. The code first uses os.listdirthe function to get the list of files in the folder, and uses sortthe method to sort the list of files by file name. Then, by traversing the file list, and using enumeratethe function and start=1parameters to set the starting number to 1, the file name and index value are obtained in sequence. Use os.path.splitextthe function to get the suffix of the file name, and concatenate the number and suffix into a new file name. Finally, use os.renamethe function to rename the original file to the new filename.

Please make sure to back up the files in the folder before running the code to prevent accidental data loss.

Before and after running:

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/BIT_HXZ/article/details/131239207