ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • <PYTHON>[절대경로, 상대경로, 현재경로]
    Flower in my dev/Python 2016. 6. 21. 17:09

    제가 필요한 것들만 테스트


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #-*- coding: utf-8 -*-
     
    import os
     
    print __file__
     
    print "1"
    cwd = os.getcwd()
    print os.getcwd()
     
    print "2"
    print os.path.dirname(__file__)
    print "3"
    print os.path.abspath(__file__)
    ab = os.path.dirname(os.path.abspath(__file__))
    print os.path.dirname(os.path.abspath(__file__))
     
    print "4"
    print os.path.join(cwd, __file__)
    print os.path.join(ab, __file__)


    cs


    스크립트를 실행하는 위치 ==> os.getcwd()


    스크립트의 상대경로 ==> os.path.dirname(__file__)


    스크립트의 절대경로 ==> os.path.abspath(__file__)


    스크립트가 있는 위치 ==> os.path.dirname(os.path.abspath(__file__))


    __file__(현재 실행되는 스크립트 파일명)는 실행하는 위치에서부터 파일명까지를 표시해주기때문에


    부모 디렉토리에서 실행했을때의 결과가 다르게 나오는 것을 주의하자.


    ==========================


    아래는 콩이랑 팥이랑 심자꾸나 님 블로그에서 퍼왔습니다.


    현재 실행되는 스크립트파일의 절대경로를 구하려면
      os.path.dirname( os.path.abspath( __file__ ) )


    두 경로의 상대적 경로 구하기 
      os.path.relpath( "c:\\ik" , "c:\\os\\pp" )
    '..\\..\\ik'

    공통경로 구하기 
     os.path.commonprefix( ["c:\\ik\\jj" , "c:\\ik\\sd" ])
    'c:\\ik\\'

    중간 경로 슬래쉬 제거 
      os.path.normpath( "c:\\os\\..\\pp" )
    'c:\\pp'

    현재 실행경로 구하기 
      os.getcwd()

    경로명 합치기 
     os.path.join( "a" , "b" ,"c" )
    'a/b/c'


    ==========================


    'Flower in my dev > Python' 카테고리의 다른 글

    <PYTHON>[Code 실행기]  (0) 2016.11.15
    <PYTHON>[python3 설치 .feat Homebrew]  (0) 2016.10.21
    <PYTHON>[byte-code(.pyc) 컴파일]  (0) 2016.05.26
    <PYTHON>[pip 설치 02]  (0) 2016.01.13
    <PYTHON>[ipython notebook 사용]  (0) 2015.11.24

    댓글

Designed by Tistory.