Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to modify file names in batches with python

Shulou Source: shulou.com Published: 2022-05-31 11:13:12 10月03日 Update

Today Xiaobian to share with you how to use python batch modification of file name related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you have some gains after reading this article, let's learn about it together.

The specific steps are as follows:

1. First, open Python and create a Python project.

2. After the python project is created, import the os module in the project using the import method;

import os

3.After the os module is imported, use the os.listdir() method to obtain the file whose file name needs to be modified;

res = os.listdir("D://text")

4. Finally, after obtaining the file, use the os.rename() method in the for loop to modify the file name in batches;

i = 0

for file in res:

orignal = "D://text" + os.sep + res[i]

new = "D://text" + os.sep + str(i+1) + ".txt"

os.rename(orignal,new)'

i += 1

The above is all the content of this article "How to modify file names in batches with python", thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: File file name knowledge article method project content module different big most that is more steps knowledge points industry information information channels logic faceted Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Microsoft macOS Docker