You can download this code by clicking the button below.
This code is now available for download.
This function generates a random filename with a prefix and a suffix, ensuring that the filename is unique.
Technology Stack : File operations (os), string operations (str), random number generation (random)
Code Type : File naming function
Code Difficulty : Intermediate
import os
import re
import sys
import time
import random
def random_filename(directory, extension):
prefix = ''.join(random.choices('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=8))
filename = f"{prefix}.{extension}"
return os.path.join(directory, filename)