开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜