[리눅스] Python 에서 스크립트가 존재하는 경로 알아내기

2023. 8. 1. 23:13Python/관리 팁

작성일 : 2017. 3. 9

 

-------------------------------- test.py -------------------------------

Python 스크립트가 존재하는 경로 : /home/user/src/test.py


CURRENT_PATH=os.getcwd()
RUN_HOME=sys.path[0]


print '==> CURRENT_PATH : '+CURRENT_PATH
print '==> RUN_HOME : '+RUN_HOME

-----------------------------------------------------------------------


함정)
os.getcwd() 는 이름 (Current Working Directory) 처럼 python 인터프리터를 호출한 위치를 반환한다.


실행 결과 #1)
$ pwd
/home/user


$ python test.py

==> CURRENT_PATH : /home/user
==> RUN_HOME : /home/user/src

실행 결과 #2)


$ pwd
/home/user/src


$ python test.py

==> CURRENT_PATH : /home/user/src
==> RUN_HOME : /home/user/src