This article mainly introduces the python implementation of file batch rename, the example code introduced in this article is very detailed, has a certain reference value, interested partners can refer to this article for everyone to share the Python implementation of file batch rename, for your reference, the specific content is as follows

1, library: OS 2, code effect: rename all files in the specified folder to 1, 2, 3… 3, code principle: use os.listdir() to traverse the file and use os.rename() to rename

code

import os def reName(dirname): count = 0 for cur_file in os.listdir(dirname): count += 1 oldDir = os.path.join(dirname, Cur_file) fileType = os.path.splitext(cur_file)[1] # filetype = os.path.splitext(cur_file) STR (count) + fileType) # new file os.rename(oldDir, newDir) print(oldDir, newDir) if __name__ == "__main__": Dirname = r""# reName(dirname)Copy the code

Running effect

Before running

Step by step print

The results