sys os - python

Get parent directory

os.path.dirname(os.getcwd())
        

Append base project directory to path

import sys
import os
from pathlib import Path
# this chaotic string grabs the project home's absolute path
base_dir = f"{str(Path.home())}/{os.path.abspath(__file__).replace(str(Path.home()), '').split('/')[1]}"
sys.path.append(base_dir)

# IMPORTANT NOTE: This will only work if operating from within a file that is in a directory in the home directory
# Also important: If using from crontab, you need to edit the crontab associated with the user where the directory lies

# Useful in the scenario where you're running a script within the
# directory and need to access modules or libraries within

# I use it for cronjobs where I also have local modules that I depend on
        

Get folder where script runs

basedir = os.path.abspath(os.path.dirname(__file__))