How to get the name of dir from where a python script is called (not exaclty where it ran)
I have a Python script, named script.py
. It's located on ~/scripts/script.py
.
I have an alias in ~/.bash_aliases
:
alias script='python ~/scripts/script.py'
I have some directories in a checked out repository, for example:
~/repository/project_dir/module_name/
I run on my terminal, inside ~/repository/project_dir/module_name/
, the script
alias I created. This script has a print statement printing the directory from where the script.py
was run, but I want it to print from where it was called. How do I do it?
(Now, I'm using os.path.abspath(sys.argv[0])
and it prints ~/scripts/script.py
instead of ~/repository/project_dir/module_nam开发者_JAVA技巧e/
)
import os
print os.getcwd()
For more details, check out the python docs.
精彩评论