You can download this code by clicking the button below.
This code is now available for download.
The function takes two arguments, uses the os module to join paths, generates a random filename, and finally creates a file in the current directory and writes random text to it.
Technology Stack : os, random, string, sys
Code Type : Code function
Code Difficulty : Intermediate
import os
import random
import string
import sys
def generate_random_string(length=10):
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(length))
def xxx(arg1, arg2):
# 使用 os.path.join 来连接文件路径
path = os.path.join(arg1, arg2)
# 使用 random 选择一个随机数作为文件名
random_filename = generate_random_string()
# 使用 string 模块中的 ascii_letters 来生成一个随机文件名
random_filename = ''.join(random.choice(string.ascii_letters) for i in range(10))
# 使用 sys 模块来获取当前工作目录
current_directory = sys.path[0]
# 使用 os.makedirs 创建一个目录
os.makedirs(current_directory, exist_ok=True)
# 使用 with 语句打开文件
with open(os.path.join(current_directory, random_filename), 'w') as file:
# 写入一些随机生成的文本
file.write(generate_random_string())