Print name, path and type

Print name, path and type

EasyOS Module Programs · 35 of 49

Write a Python program to print the name, path and type (file or folder) of everything in a folder.

Output
Name : TestFolder
Path : target\TestFolder
Type : Folder
Name : testdata.txt
Path : target\testdata.txt
Type : File

Approach

  1. os.scandir entries know their name, path and type.

Solution

Python
import os os.makedirs("target/TestFolder", exist_ok=True)open("target/testdata.txt", "w").close()with os.scandir("target") as entries:    for e in sorted(entries, key=lambda e: e.name):        print("Name :", e.name)        print("Path :", e.path)        print("Type :", "Folder" if e.is_dir() else "File")
Output
Name : TestFolder
Path : target\TestFolder
Type : Folder
Name : testdata.txt
Path : target\testdata.txt
Type : File

Learn the concept

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

More os module programs

← All 49 OS Module Programs

Print every nested file and folder

Print every nested file and folder

EasyOS Module Programs · 36 of 49

Write a Python program to print all nested files and folders with os.walk().

Output
proj/
  README.md
  src/
    main.py
    utils/
      helpers.py

Approach

  1. os.walk yields (folder, subfolders, files) for each folder.

Solution

Python
import os os.makedirs("proj/src/utils", exist_ok=True)for path in ["proj/README.md", "proj/src/main.py", "proj/src/utils/helpers.py"]:    open(path, "w").close()for folder, subfolders, files in os.walk("proj"):    depth = folder.count(os.sep)    print("  " * depth + os.path.basename(folder) + "/")    for name in sorted(files):        print("  " * (depth + 1) + name)
Output
proj/
  README.md
  src/
    main.py
    utils/
      helpers.py

Learn the concept

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

More os module programs

← All 49 OS Module Programs

Run another Python script

Run another Python script

EasyOS Module Programs · 33 of 49

Write a Python program that runs another Python script.

Output
hello from the other script
os.system returned 0
captured: hello from the other script

Approach

  1. os.system returns the exit status; subprocess.run also captures output.

Solution

Python
import osimport subprocessimport sys with open("hello_script.py", "w") as f:    f.write("print('hello from the other script')\n") status = os.system(f'"{sys.executable}" hello_script.py')print("os.system returned", status)out = subprocess.run([sys.executable, "hello_script.py"], capture_output=True, text=True)print("captured:", out.stdout.strip())
Output
hello from the other script
os.system returned 0
captured: hello from the other script

Line by line

Line 8

os.system lets the child print directly and returns its exit status.

Line 10

subprocess.run is preferred because you can capture and check the output.

Learn the concept

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

More os module programs

← All 49 OS Module Programs

Create a full path with os.makedirs()

Create a full path with os.makedirs()

EasyOS Module Programs · 32 of 49

Write a Python program to create every folder of a path with os.makedirs().

Output
True

Approach

  1. exist_ok=True makes repeated runs safe.

Solution

Python
import os os.makedirs("a/b/c/d", exist_ok=True)os.makedirs("a/b/c/d", exist_ok=True)print(os.path.isdir("a/b/c/d"))
Output
True

Learn the concept

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

More os module programs

← All 49 OS Module Programs

Change the working directory

Change the working directory

EasyOS Module Programs · 30 of 49

Write a Python program to change the current working directory.

Output
workspace
True

Approach

  1. os.chdir(); remember the old folder to go back.

Solution

Python
import os start = os.getcwd()os.makedirs("workspace", exist_ok=True)os.chdir("workspace")print(os.path.basename(os.getcwd()))os.chdir(start)print(os.getcwd() == start)
Output
workspace
True

Learn the concept

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

More os module programs

← All 49 OS Module Programs

List files with os.scandir()

List files with os.scandir()

EasyOS Module Programs · 31 of 49

Write a Python program to scan a folder and print all file names with os.scandir().

Output
['x.txt', 'y.py']

Approach

  1. scandir gives entries with name, is_file() and stat().

Solution

Python
import os os.makedirs("scan/sub", exist_ok=True)for name in ["x.txt", "y.py"]:    open(os.path.join("scan", name), "w").close()with os.scandir("scan") as entries:    print(sorted(e.name for e in entries if e.is_file()))
Output
['x.txt', 'y.py']

Learn the concept

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

More os module programs

← All 49 OS Module Programs

File size with os.path.getsize()

File size with os.path.getsize()

EasyOS Module Programs · 29 of 49

Write a Python program to get the size of a file in bytes.

Output
1000 bytes

Approach

  1. os.path.getsize().

Solution

Python
import os with open("size.bin", "wb") as f:    f.write(b"0123456789" * 100)print(os.path.getsize("size.bin"), "bytes")
Output
1000 bytes

Learn the concept

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

More os module programs

← All 49 OS Module Programs

Check read permission

Check read permission

EasyOS Module Programs · 28 of 49

Write a Python program to check read and write permission on a path with os.access().

Output
readable: True
writable: True
missing: False

Approach

  1. Pass os.R_OK / os.W_OK.

Solution

Python
import os open("perm.txt", "w").close()print("readable:", os.access("perm.txt", os.R_OK))print("writable:", os.access("perm.txt", os.W_OK))print("missing:", os.access("nope.txt", os.F_OK))
Output
readable: True
writable: True
missing: False

Learn the concept

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

More os module programs

← All 49 OS Module Programs

System and Python version

System and Python version

EasyOS Module Programs · 25 of 49

Write a Python program to get operating system and Python version information.

Output
Windows
3
True

Approach

  1. platform and sys modules.

Solution

Python
import platformimport sys print(platform.system())print(platform.python_version().split(".")[0])print(sys.version_info >= (3, 10))
Output
Windows
3
True

Learn the concept

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

More os module programs

← All 49 OS Module Programs

Folders searched for programs

Folders searched for programs

EasyOS Module Programs · 26 of 49

Write a Python program to list the folders searched for executables with os.get_exec_path().

Output
list True
True

Approach

  1. It returns the PATH folders as a list.

Solution

Python
import os paths = os.get_exec_path()print(type(paths).__name__, len(paths) > 0)print(any(os.path.isdir(p) for p in paths))
Output
list True
True
Note

The real list contains folders from your computer (often with your user name), so the example only checks its shape.

Learn the concept

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

More os module programs

← All 49 OS Module Programs