import os
import shutil
def convert_png_to_pt(folder_path):
# 获取文件夹中所有 PNG 图片文件的路径
image_files = [f for f in os.listdir(folder_path) if f.endswith(‘.png’)]
for image_file in image_files:
# 构造图片的完整路径
image_path = os.path.join(folder_path, image_file)
# 构造新的文件名,将文件格式从 .png 改为 .pt
new_image_file = image_file.replace('.png', '.pt')
# 构造新的图片的完整路径
new_image_path = os.path.join(folder_path, new_image_file)
# 使用 shutil 模块复制文件并改名
shutil.copy(image_path, new_image_path)
# 删除原始的 PNG 图片文件
os.remove(image_path)
调用示例
folder_path = ‘/path/to/folder’ # 替换为目标文件夹的路径
convert_png_to_pt(folder_path)