How do you properly determine the current script directory in Python?

后端 未结 12 1816
情深已故
情深已故 2020-11-22 02:30

I would like to see what is the best way to determine the current script directory in Python.

I discovered that, due to the many ways of calling Python code, it is ha

12条回答
  •  萌比男神i
    2020-11-22 02:47

    The os.path... approach was the 'done thing' in Python 2.

    In Python 3, you can find directory of script as follows:

    from pathlib import Path
    cwd = Path(__file__).parents[0]
    

提交回复
热议问题