[Python] Get the script execution directory with an absolute path

You can get it at the following.

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

Test code for operation check

get_abs_dirname.py


import os

print("__file__                                  : %r" %  __file__)
print("os.path.dirname(__file__)                 : %r" % (os.path.dirname(__file__)))
print("os.path.abspath(__file__)                 : %r" % (os.path.abspath(__file__)))
print("os.path.dirname(os.path.abspath(__file__)): %r" % (os.path.dirname(os.path.abspath(__file__))))

Execution result


__file__                                  : 'get_abs_dirname.py'
os.path.dirname(__file__)                 : ''
os.path.abspath(__file__)                 : '/home/nagamee/work/get_abs_dirname.py'
os.path.dirname(os.path.abspath(__file__)): '/home/nagamee/work/'


Recommended Posts

[Python] Get the script execution directory with an absolute path
Get the script path in Python
Get the return value of an external shell script (ls) with python3
Get home directory with python
Get the absolute path of the script you are running
Get the desktop path in Python
Access the file with a relative path from the execution script.
Get the weather with Python requests
Get the weather with Python requests 2
Get the desktop path in Python
[Python] Get the variable name with str
[Python] Get the files in a folder with Python
Prepare the execution environment of Python3 with Docker
Archive and compress the entire directory with python
Examine Python script bottlenecks with the cProfile module
Find the shortest path with the Python Dijkstra's algorithm
Python script to get note information with REAPER
Let's explain the asset allocation by the Black-Litterman model (with an execution example by Python)
How to get a list of files in the same directory with python
Get date with python
[Python] Get the official file path of the shortcut file (.lnk)
Get the return code of the Python script from bat
Get the result in dict format with Python psycopg2
Get the operation status of JR West with Python
Get the full path referenced by .lnk with wsl
Get an Access Token for your service account with the Firebase Admin Python SDK
Get country code with python
I replaced the Windows PowerShell cookbook with a python script.
Let's get started with Python ~ Building an environment on Windows 10 ~
Get Twitter timeline with python
POST json with Python3 script
Minimum knowledge to get started with the Python logging module
Get Youtube data with python
Creating an egg with python
Create an example game-like stage with just the Blender 2.80 script
[Python] Get the previous month
I wrote an empty directory automatic creation script in Python
Get thread ID with python
How to get into the python development environment with Vagrant
Get started with Python! ~ ② Grammar ~
Call the API with python3.
System trade starting with Python3: Get the latest program code
Get stock price with Python
Create a directory with python
Get keyboard events with python
Maya | Get the workspace path
Get Alembic information with Python
[Introduction to Python] How to get data with the listdir function
Get the source of the page to load infinitely with python.
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
Get started with the Python framework Django on Mac OS X
How to get the directory where the EXE built with Pyinstaller exists
Read a file in Python with a relative path from the program
[Python] Explains how to use the format function with an example
Try to solve the shortest path with Python + NetworkX + social data
Get the formula in an excel file as a string in Python
Get a list of files in a folder with python without a path
The fastest way to get camera images regularly with python opencv
What should I do with the Python directory structure after all?
PhytoMine-I tried to get the genetic information of plants with Python
Cut out an image with python