Fundamentals 27 min read

Master Essential Python File, Network, and Data Operations in One Guide

This comprehensive guide walks you through core Python techniques for handling files and directories, making HTTP requests, processing data, managing system resources, performing text manipulation, executing mathematical calculations, building simple web applications, and interacting with various databases, all illustrated with ready‑to‑run code snippets.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Master Essential Python File, Network, and Data Operations in One Guide

1. File and Directory Operations

Learn how to create, delete, list, copy, move, rename files and directories, as well as read, write, append, and check file existence and size using the os and shutil modules.

import os
os.makedirs('new_directory', exist_ok=True)
import shutil
shutil.rmtree('directory_to_delete')
import os
for item in os.listdir('path/to/directory'):
    print(item)
import shutil
shutil.copy('source_file.txt', 'destination_file.txt')
import shutil
shutil.move('source_file.txt', 'destination_directory/')
import os
os.rename('old_name.txt', 'new_name.txt')
with open('file.txt', 'r') as f:
    content = f.read()
    print(content)
with open('file.txt', 'w') as f:
    f.write('Hello, World!')
with open('file.txt', 'a') as f:
    f.write('
Appending new line.')
import os
if os.path.exists('file.txt'):
    print('File exists.')
import os
file_size = os.path.getsize('file.txt')
print(f'File size: {file_size} bytes')
import os
mod_time = os.path.getmtime('file.txt')
print(f'Modified time: {mod_time}')
import os
for root, dirs, files in os.walk('path/to/directory'):
    for name in files:
        print(os.path.join(root, name))
import os
for root, dirs, files in os.walk('path/to/directory'):
    if 'target_file.txt' in files:
        print(os.path.join(root, 'target_file.txt'))
import zipfile
with zipfile.ZipFile('archive.zip', 'w') as zipf:
    zipf.write('file.txt')
import zipfile
with zipfile.ZipFile('archive.zip', 'r') as zipf:
    zipf.extractall('extracted_directory')
import csv
with open('data.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)
import csv
with open('data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', 30])
    writer.writerow(['Bob', 25])
import json
with open('data.json', 'r') as f:
    data = json.load(f)
    print(data)
import json
data = {'name': 'Alice', 'age': 30}
with open('data.json', 'w') as f:
    json.dump(data, f)

2. Network Requests

Use the requests library to perform GET, POST, file download/upload, handle JSON and XML responses, set timeouts, manage errors, and maintain sessions.

import requests
response = requests.get('https://api.example.com/data')
print(response.text)
import requests
payload = {'key': 'value'}
response = requests.post('https://api.example.com/submit', data=payload)
print(response.text)
import requests
headers = {'Content-Type': 'application/json'}
response = requests.get('https://api.example.com/data', headers=headers)
print(response.text)
import requests
url = 'https://example.com/file.zip'
response = requests.get(url)
with open('downloaded_file.zip', 'wb') as f:
    f.write(response.content)
import requests
files = {'file': open('file.txt', 'rb')}
response = requests.post('https://api.example.com/upload', files=files)
print(response.text)
import requests
response = requests.get('https://api.example.com/data')
data = response.json()
print(data)
import requests
import xml.etree.ElementTree as ET
response = requests.get('https://api.example.com/data.xml')
root = ET.fromstring(response.text)
for child in root:
    print(child.tag, child.attrib)
import requests
response = requests.get('https://api.example.com/data', timeout=5)
print(response.text)
import requests
try:
    response = requests.get('https://api.example.com/data')
    response.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print('Http Error:', errh)
except requests.exceptions.ConnectionError as errc:
    print('Error Connecting:', errc)
except requests.exceptions.Timeout as errt:
    print('Timeout Error:', errt)
except requests.exceptions.RequestException as err:
    print('Oops: Something Else', err)
import requests
session = requests.Session()
session.auth = ('username', 'password')
response = session.get('https://api.example.com/data')
print(response.text)

3. Data Processing

Common string, list, dictionary, random, and datetime utilities are demonstrated.

text = "apple,banana,orange"
fruits = text.split(',')
print(fruits)
fruits = ['apple', 'banana', 'orange']
text = ','.join(fruits)
print(text)
text = "Hello, World!"
new_text = text.replace('World', 'Python')
print(new_text)
name = "Alice"
age = 30
message = f"Name: {name}, Age: {age}"
print(message)
numbers = [3, 1, 4, 1, 5, 9]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
from collections import OrderedDict
data = {'b': 2, 'a': 1, 'c': 3}
ordered_data = OrderedDict(sorted(data.items()))
print(ordered_data)
numbers = [3, 1, 4, 1, 5, 9]
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers)
from collections import Counter
text = "hello world hello"
word_counts = Counter(text.split())
print(word_counts)
import random
random_number = random.randint(1, 100)
print(random_number)
import random, string
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
print(random_string)
from datetime import datetime
now = datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_now)
from datetime import datetime
date_string = "2023-10-05 15:30:00"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print(parsed_date)
from datetime import datetime
date1 = datetime(2023, 10, 5)
date2 = datetime(2023, 10, 10)
delta = date2 - date1
print(delta.days)
from datetime import datetime, timedelta
start_date = datetime(2023, 10, 5)
end_date = datetime(2023, 10, 10)
date_range = [start_date + timedelta(days=x) for x in range((end_date - start_date).days + 1)]
for date in date_range:
    print(date.strftime("%Y-%m-%d"))
from datetime import datetime
birthdate = datetime(1990, 1, 1)
today = datetime.today()
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
print(age)

4. System Management

Execute shell commands, query environment variables, retrieve system, CPU, memory, disk, network, and process information using subprocess, os, platform, and psutil.

import subprocess
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout)
import os
current_dir = os.getcwd()
print(current_dir)
import os
os.chdir('/path/to/new/directory')
import os
value = os.getenv('PATH')
print(value)
import os
os.environ['MY_VARIABLE'] = 'my_value'
import platform
system_info = platform.uname()
print(system_info)
import psutil
cpu_percent = psutil.cpu_percent(interval=1)
print(cpu_percent)
import psutil
memory_info = psutil.virtual_memory()
print(memory_info)
import psutil
disk_usage = psutil.disk_usage('/')
print(disk_usage)
import psutil
net_if_addrs = psutil.net_if_addrs()
for interface, addrs in net_if_addrs.items():
    print(f"Interface: {interface}")
    for addr in addrs:
        print(f"  {addr.family.name}: {addr.address}")
import psutil
for proc in psutil.process_iter(['pid', 'name']):
    print(proc.info)
import psutil
for proc in psutil.process_iter(['pid', 'name']):
    if proc.info['name'] == 'process_name':
        proc.kill()
import pwd
user_info = pwd.getpwnam('username')
print(user_info)
import grp
group_info = grp.getgrnam('groupname')
print(group_info)
import logging
logging.basicConfig(filename='app.log', level=logging.INFO)
logging.info('This is an info message')

5. Text Processing

Read/write large files, perform line‑by‑line operations, search‑replace, handle CSV/JSON/XML, and use regular expressions for pattern matching, URL, email, phone, and date extraction.

with open('large_file.txt', 'r') as f:
    for line in f:
        print(line.strip())
with open('large_file.txt', 'w') as f:
    for i in range(1000000):
        f.write(f'Line {i}
')
with open('file.txt', 'r') as f:
    content = f.read()
new_content = content.replace('old_text', 'new_text')
with open('file.txt', 'w') as f:
    f.write(new_content)
import csv
with open('data.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['Name'], row['Age'])
import csv
with open('data.csv', 'w', newline='') as csvfile:
    fieldnames = ['Name', 'Age']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'Name': 'Alice', 'Age': 30})
    writer.writerow({'Name': 'Bob', 'Age': 25})
import json
with open('data.json', 'r') as f:
    data = json.load(f)
    for item in data:
        print(item['name'], item['age'])
import json
data = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
with open('data.json', 'w') as f:
    json.dump(data, f, indent=4)
import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
for child in root:
    print(child.tag, child.attrib)
import xml.etree.ElementTree as ET
root = ET.Element('root')
doc = ET.SubElement(root, 'doc')
ET.SubElement(doc, 'field1', name='name').text = 'some value1'
ET.SubElement(doc, 'field2', name='name').text = 'some value2'
tree = ET.ElementTree(root)
tree.write('output.xml')
import re
text = "The quick brown fox jumps over the lazy dog"
pattern = r'\b\w{5}\b'
matches = re.findall(pattern, text)
print(matches)
import re
text = "The quick brown fox jumps over the lazy dog"
new_text = re.sub(r'\b\w{5}\b', '*****', text)
print(new_text)
import re
text = "Visit http://example.com and https://another-example.com"
urls = re.findall(r'http[s]?://(?:[a-zA-Z0-9$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
print(urls)
import re
text = "Contact us at [email protected] or [email protected]"
emails = re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text)
print(emails)
import re
text = "Call us at 123-456-7890 or 987-654-3210"
phone_numbers = re.findall(r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', text)
print(phone_numbers)
import re
text = "Meeting on 2023-10-05 and 2023-10-10"
dates = re.findall(r'\b\d{4}-\d{2}-\d{2}\b', text)
print(dates)

6. Mathematics and Scientific Computing

Basic arithmetic with math, matrix operations with numpy, statistical analysis, linear regression with scikit‑learn, and clustering.

import math
print(math.sqrt(16))  # square root
print(math.pow(2, 3))  # exponent
print(math.sin(math.pi / 2))  # sine
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.dot(a, b)  # matrix multiplication
print(c)
import numpy as np
data = np.array([1, 2, 3, 4, 5])
mean = np.mean(data)
median = np.median(data)
std = np.std(data)
print(f"Mean: {mean}, Median: {median}, Standard Deviation: {std}")
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
model = LinearRegression()
model.fit(X, y)
print(model.coef_, model.intercept_)
from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
print(kmeans.labels_)

7. Web Development

Simple Flask applications, routing, form handling, template rendering, static file serving, and basic Django project setup with models and views.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
    return 'Hello, World!'
if __name__ == '__main__':
    app.run(debug=True)
from flask import Flask
app = Flask(__name__)
@app.route('/user/')
def show_user_profile(username):
    return f'User {username}'
if __name__ == '__main__':
    app.run(debug=True)
from flask import Flask, request
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    return f'Username: {username}, Password: {password}'
if __name__ == '__main__':
    app.run(debug=True)
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
    return render_template('index.html')
if __name__ == '__main__':
    app.run(debug=True)
from flask import Flask, send_from_directory
app = Flask(__name__)
@app.route('/static/')
def serve_static(filename):
    return send_from_directory('static', filename)
if __name__ == '__main__':
    app.run(debug=True)
bash
django-admin startproject myproject
cd myproject
python manage.py runserver
from django.db import models
class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
from django.shortcuts import render
from .models import Person
def person_list(request):
    persons = Person.objects.all()
    return render(request, 'person_list.html', {'persons': persons})

8. Database Operations

Examples for SQLite, MySQL, PostgreSQL using native drivers and SQLAlchemy ORM.

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('''CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("INSERT INTO stocks VALUES ('2023-10-05','BUY','RHAT',100,35.14)")
conn.commit()
c.execute("SELECT * FROM stocks WHERE symbol = 'RHAT'")
print(c.fetchall())
conn.close()
import mysql.connector
conn = mysql.connector.connect(host='localhost', user='yourusername', password='yourpassword', database='yourdatabase')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE stocks (date VARCHAR(255), trans VARCHAR(255), symbol VARCHAR(255), qty DOUBLE, price DOUBLE)''')
cursor.execute("INSERT INTO stocks (date, trans, symbol, qty, price) VALUES (%s, %s, %s, %s, %s)", ('2023-10-05', 'BUY', 'RHAT', 100, 35.14))
conn.commit()
cursor.execute("SELECT * FROM stocks WHERE symbol = 'RHAT'")
for row in cursor.fetchall():
    print(row)
cursor.close()
conn.close()
import psycopg2
conn = psycopg2.connect(host='localhost', database='yourdatabase', user='yourusername', password='yourpassword')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE stocks (date VARCHAR(255), trans VARCHAR(255), symbol VARCHAR(255), qty DOUBLE PRECISION, price DOUBLE PRECISION)''')
cursor.execute("INSERT INTO stocks (date, trans, symbol, qty, price) VALUES (%s, %s, %s, %s, %s)", ('2023-10-05', 'BUY', 'RHAT', 100, 35.14))
conn.commit()
cursor.execute("SELECT * FROM stocks WHERE symbol = 'RHAT'")
for row in cursor.fetchall():
    print(row)
cursor.close()
conn.close()
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///example.db')
Base = declarative_base()
class Person(Base):
    __tablename__ = 'persons'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
new_person = Person(name='Alice', age=30)
session.add(new_person)
session.commit()
persons = session.query(Person).filter_by(name='Alice').all()
for person in persons:
    print(person.id, person.name, person.age)
session.close()

9. Miscellaneous

Scheduling tasks, multithreading, multiprocessing, asynchronous programming, logging configuration, and reading INI configuration files.

import schedule, time

def job():
    print("I'm working...")

schedule.every(10).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)
import threading

def print_numbers():
    for i in range(10):
        print(i)

def print_letters():
    for letter in 'abcdef':
        print(letter)

t1 = threading.Thread(target=print_numbers)
t2 = threading.Thread(target=print_letters)
t1.start()
t2.start()
t1.join()
t2.join()
import multiprocessing

def print_numbers():
    for i in range(10):
        print(i)

def print_letters():
    for letter in 'abcdef':
        print(letter)

p1 = multiprocessing.Process(target=print_numbers)
p2 = multiprocessing.Process(target=print_letters)
p1.start()
p2.start()
p1.join()
p2.join()
import asyncio

async def print_numbers():
    for i in range(10):
        print(i)
        await asyncio.sleep(1)

async def print_letters():
    for letter in 'abcdef':
        print(letter)
        await asyncio.sleep(1)

async def main():
    task1 = asyncio.create_task(print_numbers())
    task2 = asyncio.create_task(print_letters())
    await task1
    await task2

asyncio.run(main())
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
db_host = config['DATABASE']['host']
db_port = config['DATABASE']['port']
db_user = config['DATABASE']['user']
db_password = config['DATABASE']['password']
print(f'DB Host: {db_host}, Port: {db_port}, User: {db_user}, Password: {db_password}')
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondata-processingdatabaseWeb DevelopmentNetwork RequestsFile OperationsSystem Management
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.