Pin current directory to script directory in Python

Pin the current running directory to the directory where the script is located

There are times when the runtime directory is not the directory where the script is located, such as when executing on vscode.

If you perform file processing at that time, the file will not be found and an error will occur unless the file path is set appropriately.

Then add this to the top of the file and it will work.

sample.py



import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))
  1. Get the script path with __file__
  2. Change to absolute path with ʻos.path.abspath () `
  3. Get that directory from the path with ʻos.path.dirname () `
  4. Change the current directory to that directory with ʻos.chdir () `

By doing this, the file can be read only by the file name no matter where it is executed.

Recommended Posts

Pin current directory to script directory in Python
How to know the current directory in Python in Blender
Sample script to trap signals in Python
To flush stdout in Python
Login to website in Python
A memorandum to run a python script in a bat file
Preparing to script control Rhino objects in Grasshopper / Python
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
Script to count and stop up to 5 seconds in Python in Blender
Assigned scaffolding macro in Python script file to F12 key
I wrote an empty directory automatic creation script in Python
[Python] Check the current directory, move the directory
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
Get the script path in Python
In the python command python points to python3.8
[python] Copy script to generate copy log
Try to calculate Trace in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
An alternative to `pause` in Python
Command for the current directory Python
How to pass arguments to a Python script in SPSS Modeler Batch
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
Articles to read when Blender Python script code doesn't work in 2.80
Python script written in PyTorch is converted to exe with PyInstaller
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Start the web server in the current directory
Install Pyaudio to play wave in python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
Script to generate directory from json file
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
Send email to multiple recipients in Python (Python 3)
Convert psd file to png in Python
I tried to implement PLSA in Python 2
To set default encoding to utf-8 in python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
Log in to Slack using requests in Python
Run the Python interpreter in a script
How to get a stacktrace in python
How to display multiplication table in python
Easy way to use Wikipedia in Python
How to extract polygon area in Python
How to check opencv version in python
I tried to implement ADALINE in Python
[Python] Pandas to fully understand in 10 minutes