You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
853 B
27 lines
853 B
import os
|
|
|
|
# 获取当前目录
|
|
current_directory = os.getcwd()
|
|
|
|
# 使用os.listdir()列出当前目录下的所有文件和目录
|
|
entries = os.listdir(current_directory)
|
|
|
|
# 过滤出文件
|
|
files = [entry for entry in entries if os.path.isfile(os.path.join(current_directory, entry))]
|
|
|
|
# 打印文件列表
|
|
idx = 1
|
|
for file in files:
|
|
if '.png' in file:
|
|
# 完整的原始文件路径
|
|
old_file_path = os.path.join(current_directory, file)
|
|
|
|
# 完整的新文件路径
|
|
new_file_path = os.path.join(current_directory, str(idx) + '.png')
|
|
idx = idx + 1
|
|
# 重命名文件
|
|
try:
|
|
os.rename(old_file_path, new_file_path)
|
|
print(f"文件已从 {file} 重命名为 {str(idx) + '.png'}")
|
|
except OSError as e:
|
|
print(f"重命名失败: {e}") |