English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Method to Traverse Directories and Batch Rename Files and Directories

This article describes a method to traverse directories in Python and batch rename files and directories. Share it with everyone for reference, as follows:

#encoding=utf-8
#author: walker
#date: 2014-03-07
#summary: Traverse the specified directory deeply and change the names of subdirectories and files to lowercase
#Note, this program is only for Windows, where file (folder) names are case-insensitive
import os
import os.path
import shutil
#Read in the specified directory and convert it to an absolute path
rootdir = raw_input('root dir:\n')
rootdir = os.path.abspath(rootdir)
print('absolute root path:\n*** ' + rootdir + ' ***)
#Modify the filename first
for parent, dirnames, filenames in os.walk(rootdir):
  for filename in filenames:
    pathfile = os.path.join(parent, filename)
    pathfileLower = os.path.join(parent, filename.lower())
    if pathfile == pathfileLower:  #If the filename is already all lowercase
      continue
    print(pathfile + ' --> ' + pathfileLower)
    os.rename(pathfile, pathfileLower)
#Modify the directory name later, pay attention to the topdown parameter here.
#topdown determines the order of traversal. If topdown is True, list the directories under top first, then the directories within those directories, and so on.
#On the contrary, first list the deepest subdirectories recursively, then their sibling directories, and finally the parent directory.
# We need to modify the deep subdirectories first
for parent, dirnames, filenames in os.walk(rootdir, topdown=False):
  for dirname in dirnames:
    pathdir = os.path.join(parent, dirname)
    pathdirLower = os.path.join(parent, dirname.lower())
    if pathdir == pathdirLower: # If the folder name is already all lowercase
      continue
    print(pathdir + ' --> ' + pathdirLower)
    os.rename(pathdir, pathdirLower)

Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python File and Directory Operation Skills', 'Summary of Python Text File Operation Skills', 'Summary of Python Common Traversal Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', and 'Classic Tutorial of Python Entry and Advanced'.

I hope the content described in this article will be helpful to everyone in Python program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)

You May Also Like