You can download this code by clicking the button below.
This code is now available for download.
This function takes two string arguments, creates a directory with the name of the first argument, and then creates a file within that directory with a random filename, writing the content of the second argument to the file.
Technology Stack : os, string, random
Code Type : File operation
Code Difficulty : Intermediate
import os
import sys
import random
import string
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):
if not isinstance(arg1, str) or not isinstance(arg2, str):
raise ValueError("Both arguments must be strings.")
# Create a directory with the name of the first argument
os.makedirs(arg1, exist_ok=True)
# Write a random string to a file in that directory
filename = generate_random_string(5) + '.txt'
with open(os.path.join(arg1, filename), 'w') as file:
file.write(arg2)
return os.path.join(arg1, filename)