Last modified time of a file

Last modified time of a file

EasyOS Module Programs · 45 of 49

Write a Python program to get the last modified time of a file.

Output
True

Approach

  1. os.path.getmtime.

Solution

Python
import osimport time open("timed.txt", "w").close()before = os.path.getmtime("timed.txt")time.sleep(0.05)with open("timed.txt", "a") as f:    f.write("more")print(os.path.getmtime("timed.txt") >= before)
Output
True

Line by line

Lines 6-8

Writing to the file updates its modified time.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Number of CPUs

Number of CPUs

EasyOS Module Programs · 42 of 49

Write a Python program to get the number of CPUs with os.cpu_count().

Output
int True

Approach

  1. It returns an int, or None if unknown.

Solution

Python
import os n = os.cpu_count()print(type(n).__name__, n is not None and n >= 1)
Output
int True
Note

The real number depends on your machine, which is why the example prints a check.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Split a path

Split a path

EasyOS Module Programs · 43 of 49

Write a Python program to split a path into folder and file name.

Output
('reports\\2026', 'summary.txt')
('reports\\2026\\summary', '.txt')

Approach

  1. os.path.split returns (head, tail).

Solution

Python
import os path = os.path.join("reports", "2026", "summary.txt")print(os.path.split(path))print(os.path.splitext(path))
Output
('reports\\2026', 'summary.txt')
('reports\\2026\\summary', '.txt')

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Creation (change) time of a file

Creation (change) time of a file

EasyOS Module Programs · 44 of 49

Write a Python program to get the ctime of a file with os.path.getctime().

Output
float
True

Approach

  1. The result is a timestamp; format it with datetime.

Solution

Python
import osfrom datetime import datetime open("timed.txt", "w").close()ctime = os.path.getctime("timed.txt")print(type(ctime).__name__)print(datetime.fromtimestamp(ctime).date() == datetime.now().date())
Output
float
True
Note

On Windows ctime is the creation time; on Linux it is the last metadata change.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Change root with os.chroot()

Change root with os.chroot()

EasyOS Module Programs · 41 of 49

Write a Python program that uses os.chroot() where available.

Output
os.chroot is not available on this platform

Approach

  1. chroot is Unix-only and needs root privileges, so guard it.

Solution

Python
import os if not hasattr(os, "chroot"):    print("os.chroot is not available on this platform")elif os.geteuid() != 0:    print("os.chroot needs root privileges")else:    os.chroot("/tmp")    print("root changed")
Output
os.chroot is not available on this platform
Note

chroot limits which part of the file system a process can see. It is used by system tools, rarely in everyday scripts.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Rename with os.renames()

Rename with os.renames()

EasyOS Module Programs · 40 of 49

Write a Python program to move a file into new nested folders with os.renames().

Output
True False

Approach

  1. renames creates missing folders on the new path and removes empty ones on the old path.

Solution

Python
import os os.makedirs("drafts/2025", exist_ok=True)open("drafts/2025/plan.txt", "w").close()os.renames("drafts/2025/plan.txt", "archive/2026/plan.txt")print(os.path.exists("archive/2026/plan.txt"), os.path.exists("drafts"))
Output
True False

Line by line

Line 5

The now-empty “drafts” folders are removed automatically.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Rename a folder

Rename a folder

EasyOS Module Programs · 39 of 49

Write a Python program to rename a folder with os.rename().

Output
False True

Approach

  1. os.rename(old, new).

Solution

Python
import os os.makedirs("old_name", exist_ok=True)if os.path.exists("new_name"):    os.rmdir("new_name")os.rename("old_name", "new_name")print(os.path.isdir("old_name"), os.path.isdir("new_name"))
Output
False True

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Change ownership with os.chown()

Change ownership with os.chown()

EasyOS Module Programs · 38 of 49

Write a Python program that changes file ownership with os.chown() where the platform supports it.

Output
os.chown is not available on this platform

Approach

  1. os.chown exists only on Unix systems, so check for it first.

Solution

Python
import os open("owned.txt", "w").close()if hasattr(os, "chown"):    st = os.stat("owned.txt")    os.chown("owned.txt", st.st_uid, st.st_gid)    # same owner: allowed without root    print("ownership set")else:    print("os.chown is not available on this platform")
Output
os.chown is not available on this platform

Line by line

Line 5

Changing to a different owner usually needs administrator rights; re-applying the current owner shows the call safely.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Change permissions with os.chmod()

Change permissions with os.chmod()

EasyOS Module Programs · 37 of 49

Write a Python program to change a file’s permissions with os.chmod().

Output
writable: False
writable: True

Approach

  1. Make the file read-only, check it, then make it writable again.

Solution

Python
import osimport stat open("locked.txt", "w").close()os.chmod("locked.txt", stat.S_IREAD)print("writable:", os.access("locked.txt", os.W_OK))os.chmod("locked.txt", stat.S_IREAD | stat.S_IWRITE)print("writable:", os.access("locked.txt", os.W_OK))
Output
writable: False
writable: True
Note

On Windows, chmod can only switch the read-only flag; on Linux and macOS it controls full owner, group and other permissions such as 0o755.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Create files with 10 extensions

Create files with 10 extensions

EasyOS Module Programs · 34 of 49

Write a Python program to create sample files with 10 different extensions.

Output
10 ['sample.csv', 'sample.html', 'sample.ini', 'sample.json']

Approach

  1. Loop over a list of extensions.

Solution

Python
import os os.makedirs("fake", exist_ok=True)extensions = ["txt", "csv", "json", "py", "md", "log", "xml", "html", "yaml", "ini"]for ext in extensions:    with open(os.path.join("fake", f"sample.{ext}"), "w") as f:        f.write(f"sample {ext} data\n")print(len(os.listdir("fake")), sorted(os.listdir("fake"))[:4])
Output
10 ['sample.csv', 'sample.html', 'sample.ini', 'sample.json']

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs