[PYTHON] Access files in the same directory as the executable

Introduction

You may want to access configuration files in the same directory as the executable file. I would like to summarize the common writing styles in such cases.

We are waiting for information on more efficient methods, other languages, etc.

environment

For Windows 7 and above.

C/C++

getModulePath.c


#include <windows.h>
#include <string.h>

#define BUF_SIZE 1024

TCHAR name[BUF_SIZE];
TCHAR* lastdelim;
GetModuleFileName(NULL, name, BUF_SIZE);
lastdelim = _tcsrchr(name, '\\');
*lastdelim = '\0';

C# / .NET

getModulePath.cs


using System;
using System.Reflection;
using System.IO;

Assembly assem = Assembly.GetExecutingAssembly();
String path = Path.GetDirectoryName(assem.Location);

python

getModulePath.py


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

Recommended Posts

Access files in the same directory as the executable
How to get a list of files in the same directory with python
File access under the directory
Access the Twitter API in Python
Start the web server in the current directory
Read all csv files in the folder
[Learning memo] Create if the directory does not exist / Get the files in the directory
Install the Python module in any directory
[Python] Get the files in a folder with Python
Pipfile is not created in the current directory
Extract only the file name excluding the directory in the directory
Delete all pyc files under the specified directory
Detect folders with the same image in ImageHash
How to get the files in the [Python] folder
Loop variables at the same time in the template
Batch convert PSD files in directory to PDF
The story of viewing media files in Django
Launch an HTTP server in the current directory
As a beginner, I searched the / proc directory
Access the variables defined in the script from the REPL
Unzip all zip files under the current directory
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
58 The same castle
When a local variable with the same name as a global variable is defined in the function
How to know the current directory in Python in Blender
Interrupt registration procedure as seen in the source code
Load the module with the same name in another location
Automatically access the flow in enebular and pull the trigger
Access Github by specifying the SSH key in GitPython
Floating point numbers are not the same as decimal numbers