Environment and os Module
Read env vars and paths.
Environment and os Module is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The os Module
The os module is your gateway to the operating system: environment variables, file paths, the current directory, and process info.
import os
print('os name:', os.name)Reading Environment Variables
os.environ is a dict-like mapping of environment variables. Index it to read a value.
import os
os.environ['MY_APP_MODE'] = 'demo'
print(os.environ['MY_APP_MODE'])Safe Reads with get
Indexing a missing variable raises KeyError. Use os.environ.get(name, default) for a safe lookup with a fallback.
import os
value = os.environ.get('NOT_SET', 'fallback')
print(value)
print(os.getenv('NOT_SET', 'also-fallback'))Setting Variables
Assigning to os.environ sets a variable for the current process and any child processes it spawns.
import os
os.environ['GREETING'] = 'hi'
print('GREETING' in os.environ)
print(os.environ['GREETING'])The Current Directory
os.getcwd() returns the working directory, and os.chdir() changes it.
import os
print('start in:', os.getcwd())
os.chdir('/tmp')
print('now in:', os.getcwd())Listing a Directory
os.listdir(path) returns the names in a directory. Combine it with path joining to build full paths.
import os
entries = os.listdir('/')
print('root has', len(entries), 'entries')Joining Paths Portably
Never build paths with string concatenation. os.path.join uses the correct separator for the platform.
import os
path = os.path.join('data', 'logs', 'today.txt')
print(path)Inspecting Paths
os.path has helpers to test and dissect paths: exists, isfile, isdir, basename, dirname, splitext.
import os
p = '/tmp/report.txt'
print('dir:', os.path.dirname(p))
print('name:', os.path.basename(p))
print('ext:', os.path.splitext(p)[1])Creating and Removing Directories
os.makedirs creates nested directories; exist_ok=True avoids an error if they already exist. os.rmdir removes an empty one.
import os
os.makedirs('/tmp/demo_dir/sub', exist_ok=True)
print('exists:', os.path.isdir('/tmp/demo_dir/sub'))Expanding User and Variables
os.path.expanduser('~') resolves the home directory, and os.path.expandvars substitutes environment variables in a string.
import os
os.environ['CITY'] = 'Paris'
print(os.path.expandvars('Living in $CITY'))Process and System Info
os.getpid() gives the process id and os.cpu_count() the number of CPUs, useful for sizing thread or process pools.
import os
print('pid:', os.getpid())
print('cpus:', os.cpu_count())Quick Check
Test your understanding of the os module and environment.
Recap
You learned the os module and environment:
os.environandos.getenvread and set variables safely.getcwd,chdir, andlistdirnavigate the filesystem.os.path.joinbuilds portable paths; helpers inspect them.makedirs,getpid, andcpu_countround it out.
Next: higher-level file operations with shutil.
Frequently asked questions
Is the “Environment and os Module” lesson free?
Yes — the full text of “Environment and os Module” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Environment and os Module”?
Read env vars and paths. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Environment and os Module” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Running Commands with subprocess
- Capturing Output
- Environment and os Module
- shutil for File Operations