import os
# full directory path to student pictures
student_pic_path = 'full directory path to student pics'
# get all student picture filenames from path
fnames = os.listdir(student_pic_path)
# iterate over each picture
for fname in fnames:
# split by underscore and capture student id name
new_name = fname.split('_')[0]
# get the file type
file_type = fname.split('.')[-1]
# append file type to new name
new_name = '{}.{}'.format(new_name, file_type)
os.rename(os.path.join(student_pic_path, fname),
os.path.join(student_pic_path, new_name))
您可以使用这样的方法列出目录的所有内容,提取学生id,查找文件类型,创建新名称并将其保存在同一目录中:
相关问题 更多 >
编程相关推荐