As the title suggests, if you want to get the path of the directory where the executed file is stored, how should you write it in Python3? First, ʻos.path` When using /library/os.path.html). If the acquisition path can be a relative path, write as follows.
import os.path
directory = os.path.dirname(__file__)
If you want an absolute path instead of a relative path:
import os.path
directory = os.path.dirname(os.path.abspath(__file__))
Next, consider the case of using pathlib.Path
, which is a class that abstracts the file path (ʻos.path`). Now treat the file path as a string).
First, in the case of relative paths.
import pathlib
directory = pathlib.Path(__file__).parent
Next is the absolute path.
import pathlib
directory = pathlib.Path(__file__).parent.resolve()
Whether it's ʻos.pathor
pathlib`, you can write it very simply (´ ・ ω ・ `)