Random Filename Text File Creator

  • Share this:

Code introduction


Generate a random string as the filename, create a text file and write the current time and the input arguments.


Technology Stack : random, string, datetime, os, sys, json

Code Type : File operation

Code Difficulty : Intermediate


                
                    
import random
import string
import datetime
import os
import sys
import json

def generate_random_string(length=10):
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))

def xxx(arg1, arg2):
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    filename = f"{generate_random_string(10)}.txt"
    with open(filename, 'w') as file:
        file.write(f"Current time: {current_time}\n")
        file.write(f"Input arguments: {arg1}, {arg2}\n")
    return filename