The os module talks to the operating system: where you are, what's in a folder, and creating, renaming or removing directories — the plumbing behind organising datasets.
Where am I?
import os
start = os.getcwd() # current working directory
os.mkdir("project") # create one folder
os.chdir("project") # move into it
print(os.path.basename(os.getcwd())) # project
os.chdir("..") # back to the parent
print(os.getcwd() == start) # True| Function | Does |
|---|---|
os.getcwd() | Current working directory |
os.chdir(path) | Change directory (.. = parent) |
os.listdir(path='.') | Names of files and folders in a directory |
os.mkdir(path) | Create one folder (parent must exist) |
os.makedirs(path, exist_ok=True) | Create a whole chain of folders |
os.rmdir(path) | Remove an empty folder |
os.rename(old, new) | Rename or move |
os.remove(path) | Delete a file |
os.path.join(a, b) | Build a path with the right separator |
os.path.exists(p) / isdir / isfile | Check what's there |
Nested folders
import os
os.makedirs("data/raw/2026", exist_ok=True) # creates data, data/raw and data/raw/2026
os.rmdir("data/raw/2026") # only works because it's empty
os.rename("data", "dataset")
print(sorted(os.listdir("dataset"))) # ['raw']Creating folders in bulk
import os
master = "KT"
n, m = 5, 5
for i in range(1, n + 1):
for j in range(1, m + 1):
os.makedirs(os.path.join(master, f"{master} {i}", f"{master} {i}.{j}"), exist_ok=True)
total = sum(len(dirs) for _, dirs, _ in os.walk(master)) + 1 # + the master folder itself
print(total) # 31Reading many text files
import os
os.makedirs("Files", exist_ok=True)
for name, text in [("a.txt", "first"), ("b.txt", "second"), (".DS_Store", "junk")]:
with open(os.path.join("Files", name), "w") as f:
f.write(text)
contents = []
for name in sorted(os.listdir("Files")):
if name.startswith("."): # skip hidden files like .DS_Store
continue
with open(os.path.join("Files", name)) as f:
contents.append(f.read())
print(contents) # ['first', 'second']