Lesson 44 / 49
datetime, os & sys
Work with dates, times and time differences using datetime, and use os and sys for files and arguments.
By Rahul Agarwal · 15 years in web development · Updated 1 Oct 2026
datetime basics
datetime handles dates, times and the difference between two moments.
from datetime import datetime, timedelta
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))
tomorrow = now + timedelta(days=1)
print(tomorrow.date())os & sys
os interacts with the filesystem and environment; sys exposes interpreter-level details like command-line arguments.
import os, sys
print(os.getcwd()) # current working directory
print(os.listdir(".")) # files in this folder
print(sys.argv) # command-line arguments
print(sys.version_info) # interpreter version