You can download this code by clicking the button below.
This code is now available for download.
Generate a random string of specified length containing both uppercase and lowercase letters.
Technology Stack : random, string
Code Type : Function
Code Difficulty : Intermediate
import random
import string
import re
import json
import os
import sys
import datetime
def generate_random_string(length=10):
if not isinstance(length, int) or length <= 0:
raise ValueError("Length must be a positive integer.")
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(length))
def get_size_format(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f"{s} {size_name[i]}"
def find_all_files(directory):
for root, dirs, files in os.walk(directory):
for name in files:
yield os.path.join(root, name)
def print_system_info():
info = {
'platform': sys.platform,
'python_version': sys.version,
'os_name': os.name,
'cpu_info': os.cpu_info(),
'memory_info': os.mem_info()
}
print(json.dumps(info, indent=4))
def parse_date(date_str):
try:
return datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
except ValueError:
return None
def remove_special_characters(text):
return re.sub(r'[^a-zA-Z0-9\s]', '', text)