Lesson 42 / 47

datetime, os & sys

Work with dates and times, and talk to the operating system.

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