You can download this code by clicking the button below.
This code is now available for download.
Generate a random filename with a given prefix and extension.
Technology Stack : datetime, re, hashlib, random, os, sys
Code Type : Function
Code Difficulty : Intermediate
import datetime
import re
import hashlib
import random
import os
import sys
def generate_random_filename(prefix="random_", extension=".txt"):
"""Generate a random filename with a given prefix and extension.
Args:
prefix (str): The prefix for the filename.
extension (str): The file extension.
Returns:
str: A random filename.
"""
random_str = hashlib.sha256(str(random.randint(0, 1000000)).encode()).hexdigest()[:8]
return f"{prefix}{random_str}{extension}"