Random String Generator with File and System Info

  • Share this:

Code introduction


This function generates a random string of a specified length, gets the current time, and saves this information to a file. It also checks the file size, prints the file size, and prints system information.


Technology Stack : The code uses the following packages and technologies: random, string, datetime, os, sys, json

Code Type : Function

Code Difficulty : Advanced


                
                    
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):
    # 生成一个指定长度的随机字符串
    random_string = generate_random_string(arg1)
    
    # 获取当前时间
    current_time = datetime.datetime.now()
    
    # 将字符串和当前时间保存到文件中
    with open('output.txt', 'a') as file:
        file.write(f"{random_string} {current_time}\n")
    
    # 检查文件大小
    file_size = os.path.getsize('output.txt')
    
    # 打印文件大小
    print(f"File size: {file_size} bytes")
    
    # 系统信息
    sys_info = {
        'platform': sys.platform,
        'version': sys.version
    }
    
    # 将系统信息转换为JSON格式
    json_sys_info = json.dumps(sys_info)
    
    # 打印JSON格式的系统信息
    print(json_sys_info)