Python Utility Functions Overview

  • Share this:

Code introduction


The code block contains multiple functions for generating random strings, getting file size, finding all numbers in a string, pausing the program for a while, getting the current time, and printing system information.


Technology Stack : os, re, random, string, sys, time, datetime, platform

Code Type : Function

Code Difficulty : Advanced


                
                    
import os
import re
import random
import string
import sys
import time
import datetime

def generate_random_string(length=10):
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))

def get_file_size(file_path):
    if not os.path.exists(file_path):
        return None
    return os.path.getsize(file_path)

def find_all_numbers_in_string(input_string):
    return re.findall(r'\d+', input_string)

def sleep_for_seconds(seconds):
    time.sleep(seconds)

def current_time():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

def print_system_info():
    import platform
    print(f"OS: {platform.system()}")
    print(f"Python Version: {platform.python_version()}")
    print(f"Platform Release: {platform.release()}")
    print(f"Machine: {platform.machine()}")