可以通过以下步骤实现:
import os
from tkinter import Tk, filedialog
def select_folder():
root = Tk()
root.withdraw()
folder_path = filedialog.askdirectory()
return folder_path
def change_extension(folder_path, old_extension, new_extension):
for filename in os.listdir(folder_path):
if filename.endswith(old_extension):
new_filename = os.path.splitext(filename)[0] + new_extension
old_file_path = os.path.join(folder_path, filename)
new_file_path = os.path.join(folder_path, new_filename)
os.rename(old_file_path, new_file_path)
folder_path = select_folder()
old_extension = ".jpg"
new_extension = ".png"
change_extension(folder_path, old_extension, new_extension)
这样,该程序将会遍历所选文件夹中的所有文件,如果文件的扩展名为".jpg",则将其更改为".png"。
推荐的腾讯云相关产品:对象存储(COS)
请注意,以上答案仅供参考,具体实现方式可能因个人需求和环境而异。
领取专属 10元无门槛券
手把手带您无忧上云