Common Operations with Python's os, sys, and datetime Modules
This guide demonstrates how to list directory contents, create and delete folders, manage the current working directory with the os module, retrieve interpreter information, handle command‑line arguments and exit codes using sys, and work with dates and times via the datetime module, providing expected outputs for each operation.
os module
import os print(os.listdir('.'))# lists files and sub‑directories in the current directory
Expected output: a list such as ['file1.txt', 'folder1', 'file2.py'] os.makedirs('new_folder/subfolder') # creates a multi‑level directory
Expected output: no direct output; a folder new_folder/subfolder is created. os.rmdir('new_folder') # removes an empty directory
Expected output: no direct output; the empty new_folder is deleted. print(os.getcwd()) # prints the current working directory
Expected output: a path such as
/Users/username/Documents/Projects os.chdir('/home/user')# changes the current working directory
sys module print(sys.version) Expected output: the Python interpreter version string, e.g.,
3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] import sys print(sys.argv)# prints script name and command‑line arguments
Expected output: a list like ['script.py', 'arg1', 'arg2'] when run with arguments. sys.exit(0) # exits the program with status code 0
Expected output: program terminates and returns exit code 0 to the OS. sys.path.append('/my/custom/path') # adds a directory to the module search path
Expected output: no direct output; subsequent import statements will search the new path. print(sys.ps1) # prints the current interactive prompt (default >>>) sys.ps1 = '> ' # changes the prompt
Expected output: first shows the default prompt, then the prompt changes to > in interactive mode.
datetime module
from datetime import datetime print(datetime.now())Expected output: current date‑time, e.g.,
2024-05-11 10:30:45.123456 now = datetime.now() print(now.strftime('%Y-%m-%d %H:%M:%S'))Expected output: formatted date‑time string like
2024-05-11 10:30:45 one_week_later = datetime.now() + timedelta(weeks=1) print(one_week_later)Expected output: date‑time one week ahead, e.g.,
2024-05-18 10:30:45.123456 date_string = '2024-06-01' parsed_date = datetime.strptime(date_string, '%Y-%m-%d') print(parsed_date)Expected output:
2024-06-01 00:00:00 from datetime import datetime, timedelta start = datetime(2024, 5, 1) end = datetime(2024, 5, 11) delta = end - start print(delta.days)Expected output: 10, the number of days between the two dates.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
