You can download this code by clicking the button below.
This code is now available for download.
This code defines a function to generate a random filename with a prefix and suffix, and the length of the filename does not exceed the specified maximum length.
Technology Stack : Code packages and technologies used[English]
Code Type : Function
Code Difficulty : Intermediate
import datetime
import math
import os
import random
def generate_random_filename(prefix, suffix, max_length):
"""生成一个随机文件名,包含前缀和后缀,长度不超过最大长度
Args:
prefix (str): 文件名前缀
suffix (str): 文件名后缀
max_length (int): 文件名的最大长度
Returns:
str: 生成的随机文件名
"""
filename = prefix + ''.join(random.choices('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=max_length - len(prefix) - len(suffix)))
return filename + suffix