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 .
.TIF
Two points need to be noted here - first, the requirement of this article is to change the suffix in the file name .tif
from 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 os
the module to handle file and directory operations and defined a variable folder_path
to 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 .TIF
ends with . If it .TIF
ends with, rename it. Secondly, use os.path.join()
the function to construct the old file path old_filepath
and 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
.tif
new_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_filepath
new_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