Python Standard Library for Linux: File Operations, Process Management, Networking, System Info, Time, Logging, Monitoring, Compression, and Environment Variables
This article provides a comprehensive guide to Python's standard libraries for Linux, covering file and directory manipulation, process control, socket networking, system information retrieval, date and time handling, logging, file monitoring, compression, and environment variable management with clear code examples.
1. File and Directory Operations
1.1 os
Provides OS interfaces for file and directory operations.
import os
# Get current working directory
print(os.getcwd()) # output current directory
# Change working directory
os.chdir('/path/to/new/directory')
print(os.getcwd()) # output new directory
# Create directory
os.mkdir('/path/to/new/directory')
# Remove directory
os.rmdir('/path/to/new/directory')
# List directory contents
files = os.listdir('/path/to/directory')
print(files) # output contents
# Get file size
size = os.path.getsize('/path/to/file')
print(size) # output size1.2 os.path
Provides path‑related operations.
import os.path
# Join paths
path = os.path.join('/home/user', 'documents', 'file.txt')
print(path) # /home/user/documents/file.txt
# Split path
dir, file = os.path.split('/home/user/documents/file.txt')
print(dir) # /home/user/documents
print(file) # file.txt
# Get file extension
ext = os.path.splitext('/home/user/documents/file.txt')[1]
print(ext) # .txt
# Check existence
exists = os.path.exists('/path/to/file')
print(exists) # True or False1.3 shutil
Provides high‑level file operations such as copy, move, and removal.
import shutil
# Copy file
shutil.copy('/path/to/source/file', '/path/to/destination/file')
# Move file
shutil.move('/path/to/source/file', '/path/to/destination/file')
# Delete file
os.remove('/path/to/file')
# Delete directory tree
shutil.rmtree('/path/to/directory')2. Process Management
2.1 subprocess
Used to create child processes and execute external commands.
import subprocess
# Run external command and capture output
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout) # command output
# Synchronous command
subprocess.run(['echo', 'Hello, world!'])
# Asynchronous command
process = subprocess.Popen(['sleep', '2'])
process.wait() # wait for completion3. Network Communication
3.1 socket
Creates network sockets for TCP/IP communication.
import socket
# Create TCP server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 12345))
server_socket.listen(5)
# Accept client connection
client_socket, address = server_socket.accept()
print(f"Connected by {address}")
# Receive data
data = client_socket.recv(1024)
print(f"Received: {data.decode()}")
# Send data
client_socket.sendall(b'Hello, client!')
# Close connections
client_socket.close()
server_socket.close()4. System Information
4.1 platform
Provides platform‑specific information.
import platform
print(platform.system()) # e.g., Linux
print(platform.release()) # kernel release
print(platform.version()) # full version string
print(platform.machine()) # hardware architecture4.2 psutil
Provides process and system utilization data.
import psutil
# CPU usage
print(psutil.cpu_percent(interval=1))
# Memory usage
mem_info = psutil.virtual_memory()
print(mem_info.percent)
# Disk usage
disk_info = psutil.disk_usage('/')
print(disk_info.percent)
# Network I/O
net_info = psutil.net_io_counters()
print(net_info.bytes_sent)
print(net_info.bytes_recv)5. Time and Date
5.1 datetime
Operations for dates and times.
import datetime
now = datetime.datetime.now()
print(now) # current datetime
formatted = now.strftime('%Y-%m-%d %H:%M:%S')
print(formatted) # formatted string
parsed = datetime.datetime.strptime('2023-10-01 12:34:56', '%Y-%m-%d %H:%M:%S')
print(parsed)5.2 time
Provides time‑related functions.
import time
timestamp = time.time()
print(timestamp) # epoch seconds
time.sleep(2) # pause 2 seconds
formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(formatted_time)6. Logging
Provides logging capabilities.
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='app.log',
filemode='w')
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')7. File Monitoring
7.1 watchdog
Monitors file‑system events.
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print(f"Event type: {event.event_type} path : {event.src_path}")
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='/path/to/watch', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()8. File Compression
8.1 gzip
Provides GZIP compression.
import gzip
# Compress file
with gzip.open('file.gz', 'wb') as f:
f.write(b'Hello, world!')
# Decompress file
with gzip.open('file.gz', 'rb') as f:
content = f.read()
print(content) # b'Hello, world!'9. System Environment Variables
9.1 os.environ
Accesses and modifies environment variables.
import os
# Get environment variable
print(os.environ['PATH'])
# Set environment variable
os.environ['MY_VAR'] = 'value'
# Delete environment variable
del os.environ['MY_VAR']Conclusion
Through the above sections we have detailed Python's standard libraries related to Linux, including file and directory handling, process management, network communication, system information, date and time, logging, file monitoring, compression, and environment variables, enabling more efficient Python development on Linux systems.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.