Python method to change the names or suffixes of multiple files from uppercase letters to lowercase

  This article introduces the Python language, based on a large folder , traversing multiple subfolders in it , and for a large number of files in each subfolder , batch changes the letters in the names or suffixes of the files from uppercase to lowercase . method.

  The requirements that this article hopes to achieve are: there is an existing large folder with multiple subfolders , as shown in the figure below.

Among them, there are a large number of files in each   subfolder . We can open a subfolder at will , as shown in the figure below. As shown in the purple box in the figure below, the extensions of these files are all uppercase letters (there are also some files whose current extensions are already lowercase letters); we hope that for files whose current extensions are uppercase letters , they will be Change the suffix name to lowercase letters .

.TIFTwo points need to be noted here - first, the requirement of this article is to change   the suffix in the file name .tiffrom to The idea is the same in the file name instead of the suffix name; if the letters to be modified are not sure what they are, you can achieve uppercase and lowercase conversion by modifying the ASCII code . Secondly, in the Windows operating system, the file suffix name is not case-sensitive; in Unix- like systems (such as Linux and macOS ), the file suffix name is case-sensitive; so you must modify it according to the actual situation.

  After understanding the specific needs, we can start practicing the code; the specific code used in this article is as follows.

# -*- coding: utf-8 -*-

"""
Created on Fri Sep  1 10:03:57 2023

@author: fkxxgis
"""

import os

folder_path = "E:/02_Project/202307_NDVIProduce/Beijing_Preprocessing/Four_Result/50TMK"

for root, dirs, files in os.walk(folder_path):
    for file in files:
        if file.endswith(".TIF"):
            old_filepath = os.path.join(root, file)
            new_filename = file.replace(".TIF", ".tif")
            new_filepath = os.path.join(root, new_filename)
            os.rename(old_filepath, new_filepath)

  Among them, the specific meaning of the above code is as follows.

  First, we imported osthe module to handle file and directory operations and defined a variable folder_pathto store the folder path to be traversed.

  Next, we use os.walk()the function to traverse all subfolders and subfiles under the folder path. For each file, check if its filename .TIFends with . If it .TIFends with, rename it. Secondly, use os.path.join()the function to construct the old file path old_filepathand connect the file name with the folder path where it is located; then, use the method to replace .replace()the in the file name with to get the new file name . When you apply it here, you can just modify it according to the actual situation of your own file..TIF.tifnew_filename

  We then use os.path.join()a function to build the new file path new_filepath, concatenate the new file name with the original folder path, and finally, we use a function to rename os.rename()the old file path to the new file path .old_filepathnew_filepath

  Running the above code, you can see that our file already has the modified extension.

  At this point, you're done.

Welcome to follow: Crazy Learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/132638002