You can download this code by clicking the button below.
This code is now available for download.
The following functions demonstrate the application of functions selected from Python's built-in libraries from A to Z, including generating random strings, reading file content, filtering numbers from strings, writing JSON data, printing directory contents, and getting system information.
Technology Stack : string, os, random, re, json, sys
Code Type : Function
Code Difficulty : Intermediate
import datetime
import json
import os
import random
import re
import string
import sys
def generate_random_string(length=10):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def read_file_content(file_path):
with open(file_path, 'r') as file:
return file.read()
def filter_numbers_from_string(input_string):
return re.findall(r'\d+', input_string)
def write_json_data(data, file_path):
with open(file_path, 'w') as file:
json.dump(data, file, indent=4)
def print_directory_contents(path='.'):
for root, dirs, files in os.walk(path):
level = root[len(path):].count(os.sep)
indent = ' ' * 4 * level
print(f"{indent}{os.path.basename(root)}/")
subindent = ' ' * 4 * (level + 1)
for f in files:
print(f"{subindent}{f}")
def get_system_info():
info = {
'system': os.name,
'platform': sys.platform,
'python_version': sys.version,
'machine': sys_machine
}
return info