如何知道调用 shell 目录的路径并在我的 python 可执行文件中使用它

How to know path of calling shell directory and use it in my python executable

基本上,我想使用 shell 从任何地方调用 python 程序,所以我添加了一个 shebang 并将其复制到 /usr/local/bin 并具有可执行权限。 python 程序接受一个命令行参数,它是输入文件的相对路径。
我被困在这里,我不知道该怎么做才能获得 shell 的绝对路径。我假设一旦我以某种方式获得绝对路径,我可以使用 sys.argv[1] 来获取输入的文件相对路径(我将附加到 shell 工作目录的绝对路径)但请做正确的我这行不通。

您可以通过 os.environ['PWD'] 打印工作目录。 your_script.py的内容:

#!/usr/bin/python3.5

import os
print(os.environ['PWD'])

用法:

sanyash@sanyash-ub16:/etc/nginx$ your_script.py 
/etc/nginx
sanyash@sanyash-ub16:/etc/nginx$