Avoiding Hard Coding to get Python file paths
Question:
I am working on the files with different locations. I want to avoid hard coding at every step. My files Involves, Input files(consisting of inputs), python scripts and output files. Now, I want to interact with each of the mentioned folder without hard-coding my scripts. Does python has any module for trimming down the path to root directory and accessing the subsequent folders.
Currently, I am using os.getcwd() for accessing my script’s path. I want to use the output of this code for accessing other files in the different folder
import os
dirpath = os.getcwd()
root_dirpath, py_script = os.path.split(dirpath)
root_dirpath in the code gives me the path for my python script. I want to trim this path to user folder and then access the file from the other folder. Is there any module to do this with out actually hard coding.
Answers:
There is current-working-directory,
and then there is the directory your program is in.
They are distinct concepts, and might have the same value or different.
This expression is pretty useful:
os.path.dirname(os.path.realpath(__file__))
Or more simply, just take dirname of __file__
if your $PYTHONPATH
lacks troublesome entries like ..
Some projects will have a "top" directory that is one or more levels up
from the location of your python source.
You might find it useful to append '../../..'
to __file__
,
or use os.path.join()
to accomplish the same thing.
Here is one possibility for locating your favorite files:
from pathlib import Path
top = Path(__file__ + '../..').resolve()
arrow = top / 'assets/arrow.png'
print('source image:', arrow)
print('destination image:', top / 'output' / os.path.basename(arrow))
Caveat: sometimes library code runs directly from a .zip file
or other unusual environment.
In that case you might find __file__
is not quite as useful.
I am working on the files with different locations. I want to avoid hard coding at every step. My files Involves, Input files(consisting of inputs), python scripts and output files. Now, I want to interact with each of the mentioned folder without hard-coding my scripts. Does python has any module for trimming down the path to root directory and accessing the subsequent folders.
Currently, I am using os.getcwd() for accessing my script’s path. I want to use the output of this code for accessing other files in the different folder
import os
dirpath = os.getcwd()
root_dirpath, py_script = os.path.split(dirpath)
root_dirpath in the code gives me the path for my python script. I want to trim this path to user folder and then access the file from the other folder. Is there any module to do this with out actually hard coding.
There is current-working-directory,
and then there is the directory your program is in.
They are distinct concepts, and might have the same value or different.
This expression is pretty useful:
os.path.dirname(os.path.realpath(__file__))
Or more simply, just take dirname of __file__
if your $PYTHONPATH
lacks troublesome entries like ..
Some projects will have a "top" directory that is one or more levels up
from the location of your python source.
You might find it useful to append '../../..'
to __file__
,
or use os.path.join()
to accomplish the same thing.
Here is one possibility for locating your favorite files:
from pathlib import Path
top = Path(__file__ + '../..').resolve()
arrow = top / 'assets/arrow.png'
print('source image:', arrow)
print('destination image:', top / 'output' / os.path.basename(arrow))
Caveat: sometimes library code runs directly from a .zip file
or other unusual environment.
In that case you might find __file__
is not quite as useful.