I’m looking for the most Pythonic and reliable way to set the path of the working directory.
- Using
__file__
:
import os
script_dir = os.path.dirname(os.path.abspath(__file__))
- Using
sys
module:
import os
import sys
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
- Using
Path
frompathlib
:
from pathlib import Path
script_dir = Path(__file__).resolve().parent
New contributor
selah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.