Introduction to the method of batch modifying file names in Python using the os module (with code)

不言
Release: 2019-04-15 10:36:13
forward
7953 people have browsed it

This article brings you an introduction to how Python uses the os module to batch modify file names (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Beginner to learn Python. Write down your own small exercises in the essay.

You can do it by consulting the information of rename and renames in the os module

The difference between them is.rename: can only Modify the file name renames: You can modify the file name, and you can also modify the name of the upper-level directory of the file

Another method used is os.listdir(path) path is the path This method can modify the file in the specified path folder Enter the name into a list

The following is the code:

import os
path = "e:/test/"    # 目标路径

"""os.listdir(path) 操作效果为 返回指定路径(path)文件夹中所有文件名"""
filename_list = os.listdir(path)  # 扫描目标路径的文件,将文件名存入列表
 
 a = 0
for i in filename_list:
    used_name = path + filename_list[a]
    new_name = path + "new_" + filename_list[a]
     os.rename(used_name,new_name)
    print("文件%s重命名成功,新的文件名为%s" %(used_name,new_name))
     a += 1
Copy after login

The running effect is:

Before renaming:

After renaming:

## [Related recommendation: python tutorial]

The above is the detailed content of Introduction to the method of batch modifying file names in Python using the os module (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template