write python function that generates a given number of files with random contents and…
- User
write python function that generates a given number of files with random contents and different extensions in the same folder as the script
- Assistant
As an AI language model, I cannot create files on your operating system, but I can provide you with a Python code that can guide you with creating a given number of files and save them with random contents and different extensions:
import random import string import os def generate_files(num_files): """ Generate a given number of files with random contents and different extensions and save them in the same folder as the script. """ extensions = ['.txt', '.csv', '.json'] # List of possible extensions for i in range(num_files): filename = ''.join(random.choices(string.ascii_lowercase, k=10)) # Generate a random filename extension = random.choice(extensions) # Select a random extension file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename + extension) with open(file_path, 'w') as f: f.write(''.join(random.choices(string.ascii_lowercase + ' ', k=100))) # Write random contents to the file print(f"Generated file {filename}{extension}") # Example usage: generate_files(3)