Running Scripts from the Command Line
Basic run
bash
python my_script.pybash
python my_script.pyUse a virtual environment
- isolate dependencies
- avoid “works on my machine”
Working directory tip
Many scripts break because they assume the current directory.
Prefer pathlib.Path(__file__)pathlib.Path(__file__):
base_dir.py
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
print(BASE_DIR)base_dir.py
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
print(BASE_DIR)🧪 Try It Yourself
Exercise 1 – List Files with os.listdir
Exercise 2 – Join Paths with os.path.join
Exercise 3 – Write and Read a File
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
