You can download this code by clicking the button below.
This code is now available for download.
The following code block contains multiple functions implemented using Python's built-in libraries, covering the usage of Python's built-in libraries from A to Z.
Technology Stack : random, os, string, datetime, math, json
Code Type : Code collection
Code Difficulty : Advanced
import random
import os
import string
import datetime
import math
import json
def generate_random_string(length):
if not isinstance(length, int) or length <= 0:
raise ValueError("Length must be a positive integer")
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(length))
def get_current_time():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def calculate_circle_area(radius):
if radius <= 0:
raise ValueError("Radius must be a positive number")
return math.pi * (radius ** 2)
def save_data_to_file(data, file_name):
with open(file_name, 'w') as file:
json.dump(data, file)
def read_data_from_file(file_name):
with open(file_name, 'r') as file:
return json.load(file)
def list_files_in_directory(directory):
return os.listdir(directory)
def sort_list_by_length(lst):
return sorted(lst, key=len)
def find_prime_numbers(n):
if n <= 1:
return []
primes = []
for possible_prime in range(2, n + 1):
is_prime = True
for num in range(2, int(math.sqrt(possible_prime)) + 1):
if possible_prime % num == 0:
is_prime = False
break
if is_prime:
primes.append(possible_prime)
return primes
def create_directory(directory_name):
if not os.path.exists(directory_name):
os.makedirs(directory_name)
def remove_directory(directory_name):
if os.path.exists(directory_name):
os.rmdir(directory_name)