Running Scripts from the Command Line
Basic run
python my_script.pypython 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
