[PYTHON] I want to get the path of the directory where the running file is stored.

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.pathorpathlib`, you can write it very simply (´ ・ ω ・ `)

Recommended Posts

I want to get the path of the directory where the running file is stored.
To get the path of the currently running python.exe
I want to get the operation information of yahoo route
Keras I want to get the output of any layer !!
I want to get the name of the function / method being executed
Get the path to the systemd unit file
I want to get League of Legends data ③
I want to get League of Legends data ②
I want to customize the appearance of zabbix
I want to get League of Legends data ①
Get the region where AWS Lambda is running
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to add silence to the beginning of a wav file for 1 second
I want to get the file name, line number, and function name in Python 3.4
I want to see the file name from DataLoader
I want to grep the execution result of strace
Around the place where the value of Errbot is stored
[Python] Get the official file path of the shortcut file (.lnk)
Get the absolute path of the script you are running
I want to fully understand the basics of Bokeh
I want to increase the security of ssh connections
Get the location of the file where the exe is placed when the exe created by PyInstaller is executed
I want to get started with the Linux kernel, what is the list head structure?
I want to initialize if the value is empty (python)
I tried to get the location information of Odakyu Bus
I want to use only the normalization process of SudachiPy
I want to judge the authenticity of the elements of numpy array
I want to know the features of Python and pip
I want to know the legend of the IT technology world
Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
Get absolute path of running process
Get the file path using Pathlib
Set the last modified date of the child file to the modified date of the parent directory
I want to manually assign the training parameters of the [Pytorch] model
I tried to get the index of the list using the enumerate function
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
I want to output the beginning of the next month with Python
How to get the directory where the EXE built with Pyinstaller exists
I want to check the position of my face with OpenCV!
I want to know the population of each country in the world.
When I try to run the pip command after updating pip, I get "No such file or directory"
I want to extract the tag information (title and artist) of a music file (flac, wav).
I want to pin Spyder to the taskbar
I want to get angry with my mom when my memory is tight
[Python Kivy] How to get the file path by dragging and dropping
I want to output to the console coolly
I tried to get the batting results of Hachinai using image processing
[Note] I want to completely preprocess the data of the Titanic issue-Age version-
[For beginners] I want to get the index of an element that satisfies a certain conditional expression
I want to handle the rhyme part1
I don't want to admit it ... The dynamical representation of Neural Networks
If you just want to get the dump file of the server, it was convenient to build an http server
I want to handle the rhyme part3
(Python Selenium) I want to check the settings of the download destination of WebDriver
I measured 6 methods to get the index of the maximum value (minimum value) of the list
I want to receive the configuration file and check if the JSON file generated by jinja2 is a valid JSON
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I tried to get the authentication code of Qiita API with Python.
I want to sort a list in the order of other lists
View the full path (absolute path) of a file in a directory in Linux Bash