简体   繁体   中英

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

Basically, I want to call a python program from anywhere using shell so I added a shebang and copied it to /usr/local/bin with executable permission. The python program takes a command line argument which is the relative path of an input file.
I am stuck here, I have no idea what to do so that I can obtain the absolute path of the shell. I am assuming once I get the absolute path somehow, I can use sys.argv[1] to get the entered relative path of the file(which I will append to the absolute path of shell working directory) but please do correct me it won't work.

You can Print Working Directory via os.environ['PWD'] . Content of your_script.py :

#!/usr/bin/python3.5

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

Usage:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM