You can download this code by clicking the button below.
This code is now available for download.
This function generates a random filename and specifies the directory and file extension.
Technology Stack : os, random
Code Type : Code generation
Code Difficulty :
import os
import random
import json
import re
def random_filename(directory, extension='.txt'):
"""
生成一个随机文件名。
:param directory: 文件存储的目录
:param extension: 文件扩展名,默认为.txt
:return: 生成的随机文件名
"""
filename = f"{random.randint(1000, 9999)}{extension}"
return os.path.join(directory, filename)