Python 最近用Python上传文件,用到判断文件是否存在,如果存在则自动(+1)
def file_check(file_name):
temp_file_name = file_name
i = 1
while i:
print(temp_file_name)
print(os.path.exists("static/" + temp_file_name))
if os.path.exists("static/" + temp_file_name):
name, suffix = file_name.split('.')
name += '(' + str(i) + ')'
temp_file_name = name+'.'+suffix
i = i+1
else:
return temp_file_name
本文介绍如何使用Python实现文件上传时,通过检查文件是否存在并按序号自动重命名,确保文件名唯一。通过`os.path.exists`判断文件路径,采用循环和字符串操作来生成新的文件名。
3298

被折叠的 条评论
为什么被折叠?



