d:\Temp 資料夾
底下的檔案如下:
code:
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 4 18:26:15 2023
@author: SavingKing
"""
import glob
import os
dirname = r"D:\Temp" #使用者自行修改
basename ="*.txt" #使用者自行修改
fpath = os.path.join(dirname , basename)
#'D:\\Temp\\*.txt'
original_str = "ex_pn" #使用者自行修改
new_str = "VIZ_1395A" #使用者自行修改
lis_fpath = glob.glob(fpath)
# ['D:\\Temp\\ex_pn_SPEC1.txt', 'D:\\Temp\\ex_pn_SPEC2.txt']
# lis_new_path = []
for path in lis_fpath:
new_path = path.replace(original_str , new_str)
# lis_new_path.append(new_path)
os.rename(path , new_path)
程式執行後:
順便示範以下幾個函式:
推薦hahow線上學習python: https://igrape.net/30afN