Python Time Management: A Beginner’s Guide to Keeping Time, Scheduling Tasks, and Launching Programs
Learn Python time management, task scheduling, and program launching with clear examples. Follow along as I explore and share what I learn.
code
python
Author
Steven P. Sanderson II, MPH
Published
October 23, 2025
Keywords
Programming, Python time management, Python task scheduling, Python schedule library, Launching programs with Python, Automate tasks in Python, Python subprocess module, Python datetime module, Python time module, Schedule tasks in Python, Python automation scripts, How to schedule recurring tasks in Python, Automate backups using Python schedule library, Launch external programs with Python subprocess, Beginner’s guide to Python time and scheduling, Step-by-step Python task automation tutorial
Author’s Note: As I write this series on Python programming, I’m learning alongside you every step of the way. My goal is to share what I learn and present it to you in a simple manner. While I research and test each concept, I’m constantly expanding my own understanding of these topics, this means I might make mistakes or do things in a manner that is not best practice. Your feedback, questions, and insights are not only welcome but to me, incredibly valuable.
Key Takeaway: Master Python’s built-in time functions, automate your tasks with smart scheduling, and seamlessly launch external programs to boost your programming productivity.
Python offers powerful built-in tools for managing time, scheduling tasks, and launching external programs. Whether you’re building automated scripts, creating scheduled backups, or integrating with other applications, these important skills will transform how you approach programming projects.
Understanding Python’s Time Management Tools
Python provides several modules for working with time and dates. Let’s explore the most important ones for beginners:
The time Module: Your Basic Timekeeping Toolkit
The time module handles basic time operations like measuring duration, pausing execution, and working with timestamps.
Key time Functions:
Function
Purpose
Example Usage
time.time()
Get current timestamp
start = time.time()
time.sleep(seconds)
Pause execution
time.sleep(2)
time.ctime()
Convert timestamp to readable string
time.ctime() → 'Tue Oct 21 03:26:00 2025'
import time# Measure how long code takes to runstart_time = time.time()# Your code hereend_time = time.time()print(f"Execution time: {end_time - start_time} seconds")# Pause execution for 3 secondstime.sleep(3)print("This prints after 3 seconds!")
Execution time: 6.842613220214844e-05 seconds
This prints after 3 seconds!
The datetime Module: Advanced Date and Time Manipulation
For more complex date operations, the datetime module provides powerful tools for creating, formatting, and calculating with dates.
Key datetime Classes:
datetime.now(): Get current date and time
datetime.strftime(): Format dates as strings
datetime.strptime(): Parse strings into date objects
timedelta: Represent time durations
from datetime import datetime, timedelta# Get current date and timenow = datetime.now()print(now.strftime("%Y-%m-%d %H:%M:%S"))# Calculate future datestomorrow = now + timedelta(days=1)next_week = now + timedelta(weeks=1)
2025-10-23 09:38:10
Scheduling Tasks with Python
Using the schedule Library for Simple Task Automation
The schedule library makes it incredibly easy to run Python functions at specific times or intervals.
For simple recurring tasks, you can create basic schedulers without external libraries:
import timefrom datetime import datetimedef simple_scheduler(): last_run = time.time() interval =60# Run every 60 secondswhileTrue: current_time = time.time()if current_time - last_run >= interval:print(f"Task executed at {datetime.now().strftime('%H:%M:%S')}") last_run = current_time time.sleep(1)
Launching External Programs
Using subprocess for Program Execution
The subprocess module is the recommended way to launch external programs from Python.
Key subprocess Functions:
Function
Purpose
Use Case
subprocess.run()
Execute command and wait
Simple one-time commands
subprocess.Popen()
Advanced process control
Background processes, real-time interaction
subprocess.check_output()
Get command output only
When you only need the result
Basic Program Launch:
import subprocess# Run a Python scriptresult = subprocess.run(['python', 'my_script.py'], capture_output=True, text=True)if result.returncode ==0:print("Success:", result.stdout)else:print("Error:", result.stderr)
Important Parameters:
capture_output=True: Capture the program’s output
text=True: Get output as strings (not bytes)
timeout=5: Prevent hanging by setting a time limit
check=True: Raise an exception if the program fails
Security Best Practices
Always use command lists instead of strings to prevent security vulnerabilities:
# ✅ Safe waysubprocess.run(['ls', '-l', '/home/user'])# ❌ Dangerous way (vulnerable to injection)subprocess.run("ls -l /home/user", shell=True)
Practical Applications: Putting It All Together
Automated Backup Script with Logging
Here’s a complete example combining time management, scheduling, and program launching:
import subprocessimport timefrom datetime import datetimeimport scheduledef create_backup():"""Create a timestamped backup""" timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") backup_name =f"backup_{timestamp}.tar.gz"print(f"Starting backup at {datetime.now().strftime('%H:%M:%S')}")# Measure backup time start_time = time.time()# Create backup (example command)try: result = subprocess.run(['tar', '-czf', backup_name, '/path/to/data' ], capture_output=True, text=True, timeout=300) end_time = time.time() duration = end_time - start_timeif result.returncode ==0:print(f"Backup completed in {duration:.2f} seconds") log_backup_success(backup_name, duration)else:print(f"Backup failed: {result.stderr}")except subprocess.TimeoutExpired:print("Backup timed out after 5 minutes")def log_backup_success(filename, duration):"""Log successful backup to file"""withopen('backup_log.txt', 'a') as f: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") f.write(f"[{timestamp}] Backup created: {filename} ({duration:.2f}s)\n")# Schedule daily backupsschedule.every().day.at("02:00").do(create_backup)
Your Turn!
Challenge: Create a simple system monitor that checks disk space every 5 minutes and logs the results.
Requirements:
Use subprocess to run a disk space command
Schedule the check to run every 5 minutes
Log results with timestamps
Display results in a readable format
Click here for Solution!
import subprocessimport timefrom datetime import datetimeimport scheduledef check_disk_space():"""Monitor disk space and log results"""try:# Get disk usage (Unix/Linux command) result = subprocess.run(['df', '-h'], capture_output=True, text=True, timeout=10)if result.returncode ==0: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")# Parse the output lines = result.stdout.strip().split('\n') root_line = [line for line in lines if line.endswith('/')][0] usage = root_line.split()[4] # Get usage percentage# Log to filewithopen('disk_monitor.log', 'a') as f: f.write(f"[{timestamp}] Disk Usage: {usage}\n")print(f"Disk usage logged: {usage} at {timestamp}")# Alert if usage is highifint(usage.strip('%')) >80:print("⚠️ Warning: Disk usage above 80%!")exceptExceptionas e:print(f"Error checking disk space: {e}")# Schedule the checkschedule.every(5).minutes.do(check_disk_space)# Run schedulerprint("Starting disk space monitor...")whileTrue: schedule.run_pending() time.sleep(1)
Quick Takeaways
Use time.time() and time.sleep() for basic timing and delays in your programs
Master datetime for working with dates, formatting, and date arithmetic
Install and use the schedule library for easy task automation with human-readable syntax
Always use subprocess.run() instead of older methods like os.system() for launching programs
Use command lists, not strings with subprocess for better security
Capture output with capture_output=True when you need to process program results
Set timeouts to prevent your programs from hanging on external commands
Combine these tools to create powerful automation scripts for backups, monitoring, and maintenance
Conclusion
Python’s time management and process control capabilities make it an excellent choice for automation and system administration tasks. By mastering the time and datetime modules for timekeeping, the schedule library for task automation, and the subprocess module for program launching, you’ll be able to build sophisticated scripts that handle complex workflows.
Start small with simple scheduled tasks, then gradually incorporate external program launches as you become more comfortable. The combination of these tools will dramatically expand what you can accomplish with Python automation.
Ready to automate your workflow? Pick one repetitive task you do regularly and try scheduling it with Python. Your future self will thank you for the time saved!