문제

다른 스크립트 파일을 호출하는 스크립트가 있지만 현재 프로세스 내에서 실행 중인 파일의 파일 경로를 가져와야 합니다.

예를 들어 세 개의 파일이 있다고 가정해 보겠습니다.사용 실행 파일:

  • script_1.py 전화 script_2.py.
  • 차례로, script_2.py 전화 script_3.py.

파일 이름과 경로를 어떻게 알 수 있나요? script_3.py, 내부 코드에서 script_3.py, 해당 정보를 인수로 전달할 필요 없이 script_2.py?

(실행 중 os.getcwd() 현재 파일이 아닌 원래 시작 스크립트의 파일 경로를 반환합니다.)

도움이 되었습니까?

해결책

p1.py:

execfile("p2.py")

p2.py:

import inspect, os
print inspect.getfile(inspect.currentframe()) # script filename (usually with path)
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory

다른 팁

__file__

다른 사람들이 말했듯이.당신은 또한 사용하고 싶을 수도 있습니다 os.path.realpath 심볼릭 링크를 제거하려면:

import os

os.path.realpath(__file__)

2018년 11월 28일 업데이트:

다음은 Python 2와 3을 사용한 실험을 요약한 것입니다.와 함께

main.py - foo.py를 실행합니다.
foo.py - lib/bar.py를 실행합니다.
lib/bar.py - 파일 경로 표현식을 인쇄합니다.

| Python | Run statement       | Filepath expression                    |
|--------+---------------------+----------------------------------------|
|      2 | execfile            | os.path.abspath(inspect.stack()[0][1]) |
|      2 | from lib import bar | __file__                               |
|      3 | exec                | (wasn't able to obtain it)             |
|      3 | import lib.bar      | __file__                               |

Python 2의 경우 패키지로 전환하는 것이 더 명확할 수 있으므로 다음을 사용할 수 있습니다. from lib import bar - 그냥 공백으로 추가하세요 __init__.py 두 폴더에 파일을 넣으세요.

파이썬 3의 경우, execfile 존재하지 않습니다. 가장 가까운 대안은 다음과 같습니다. exec(open(<filename>).read()), 그러나 이는 스택 프레임에 영향을 미칩니다.그냥 사용하는 것이 가장 간단합니다 import foo 그리고 import lib.bar - 아니요 __init__.py 필요한 파일.

또한보십시오 가져오기와 실행 파일의 차이점


원래 답변:

다음은 Windows에서 Python 2.7.10을 사용하여 이 스레드의 답변을 기반으로 한 실험입니다.

스택 기반의 것들은 신뢰할 수 있는 결과를 제공하는 유일한 것 같습니다.마지막 두 개는 구문이 가장 짧습니다., 즉.-

print os.path.abspath(inspect.stack()[0][1])                   # C:\filepaths\lib\bar.py
print os.path.dirname(os.path.abspath(inspect.stack()[0][1]))  # C:\filepaths\lib

여기에 추가되는 내용은 다음과 같습니다. 시스템 기능으로!@Usagi 및 @pablog에 대한 크레딧

다음 세 파일을 기반으로 해당 폴더에서 main.py를 실행합니다. python main.py (또한 절대 경로를 사용하여 별도의 폴더에서 호출하여 execfile을 시도했습니다).

C:\파일 경로\main.py: execfile('foo.py')
C:\파일 경로\foo.py: execfile('lib/bar.py')
C:\파일 경로\lib\bar.py:

import sys
import os
import inspect

print "Python " + sys.version
print

print __file__                                        # main.py
print sys.argv[0]                                     # main.py
print inspect.stack()[0][1]                           # lib/bar.py
print sys.path[0]                                     # C:\filepaths
print

print os.path.realpath(__file__)                      # C:\filepaths\main.py
print os.path.abspath(__file__)                       # C:\filepaths\main.py
print os.path.basename(__file__)                      # main.py
print os.path.basename(os.path.realpath(sys.argv[0])) # main.py
print

print sys.path[0]                                     # C:\filepaths
print os.path.abspath(os.path.split(sys.argv[0])[0])  # C:\filepaths
print os.path.dirname(os.path.abspath(__file__))      # C:\filepaths
print os.path.dirname(os.path.realpath(sys.argv[0]))  # C:\filepaths
print os.path.dirname(__file__)                       # (empty string)
print

print inspect.getfile(inspect.currentframe())         # lib/bar.py

print os.path.abspath(inspect.getfile(inspect.currentframe())) # C:\filepaths\lib\bar.py
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # C:\filepaths\lib
print

print os.path.abspath(inspect.stack()[0][1])          # C:\filepaths\lib\bar.py
print os.path.dirname(os.path.abspath(inspect.stack()[0][1]))  # C:\filepaths\lib
print

나는 이것이 더 깨끗하다고 ​​생각합니다.

import inspect
print inspect.stack()[0][1]

다음과 같은 정보를 얻습니다.

print inspect.getfile(inspect.currentframe())

여기서 [0]은 스택의 현재 프레임(스택 상단)이고 [1]은 파일 이름입니다. 즉, 스택에서 뒤로 이동하려면 증가합니다.

print inspect.stack()[1][1]

현재 프레임을 호출한 스크립트의 파일 이름이 됩니다.또한 [-1]을 사용하면 원래 호출 스크립트인 스택의 맨 아래로 이동하게 됩니다.

import os
os.path.dirname(__file__) # relative directory path
os.path.abspath(__file__) # absolute file path
os.path.basename(__file__) # the file name only

최고로 표시된 제안은 스크립트가 하나의 파일로만 구성된 경우 모두 사실입니다.

실행 파일의 이름을 알고 싶다면(예:현재 프로그램의 Python 인터프리터에 전달된 루트 파일)을 모듈로 가져올 수 있는 파일에서 이 작업을 수행해야 합니다(이 파일이 foo.py):

import inspect

print inspect.stack()[-1][1]

왜냐하면 마지막 것([-1])가 스택에 가장 먼저 들어갑니다(스택은 LIFO/FILO 데이터 구조입니다).

그런 다음 파일에 bar.py 만약 너라면 import foo 인쇄될 거예요 bar.py,보다는 foo.py, 이는 다음 모두의 값이 됩니다.

  • __file__
  • inspect.getfile(inspect.currentframe())
  • inspect.stack()[0][1]
import os
print os.path.basename(__file__)

그러면 파일 이름만 제공됩니다.즉.파일의 절대 경로가 c:\abcd\abc.py이면 두 번째 줄은 abc.py를 인쇄합니다.

"현재 프로세스 내에서 실행 중인 파일의 파일 경로"가 무엇을 의미하는지 완전히 명확하지 않습니다.sys.argv[0] 일반적으로 Python 인터프리터가 호출한 스크립트의 위치를 ​​포함합니다.을 체크 해봐 시스템 문서 상세 사항은.

@Tim과 @Pat Notz가 지적했듯이 __file__ 속성은 다음에 대한 액세스를 제공합니다.

파일에서로드 된 경우 모듈이로드 된 파일

Windows 환경에서 작동해야 하는 스크립트가 있습니다.잘라낸 이 코드는 내가 완성한 것입니다.

import os,sys
PROJECT_PATH = os.path.abspath(os.path.split(sys.argv[0])[0])

그것은 꽤 해키적인 결정입니다.하지만 외부 라이브러리가 필요하지 않으며 제 경우에는 이것이 가장 중요합니다.

import os
os.path.dirname(os.path.abspath(__file__))

검사나 다른 라이브러리가 필요하지 않습니다.

이는 가져온 스크립트와 동일한 폴더에 있는 구성 파일을 사용하는 스크립트(실행된 스크립트와 다른 디렉터리에서)를 가져와야 할 때 효과적이었습니다.

그만큼 __file__ 속성은 기본 실행 코드가 포함된 파일과 가져온 모듈 모두에 대해 작동합니다.

보다 https://web.archive.org/web/20090918095828/http://pyref.infogami.com/__file__

import sys

print sys.path[0]

현재 실행 중인 스크립트의 경로가 인쇄됩니다.

내 생각엔 그냥 __file__ 확인해 보시는 것도 좋을 것 같습니다. 모듈 검사.

당신이 사용할 수있는 inspect.stack()

import inspect,os
inspect.stack()[0]  => (<frame object at 0x00AC2AC0>, 'g:\\Python\\Test\\_GetCurrentProgram.py', 15, '<module>', ['print inspect.stack()[0]\n'], 0)
os.path.abspath (inspect.stack()[0][1]) => 'g:\\Python\\Test\\_GetCurrentProgram.py'

이 시도,

import os
os.path.dirname(os.path.realpath(__file__))
import sys
print sys.argv[0]

이것은 작동합니다:

import os,sys
filename=os.path.basename(os.path.realpath(sys.argv[0]))
dirname=os.path.dirname(os.path.realpath(sys.argv[0]))

스크립트 실행 디렉토리를 얻으려면

 print os.path.dirname( inspect.getfile(inspect.currentframe()))

플랫폼(macOS/Windows/Linux) 전반에서 마이그레이션 일관성을 유지하려면 다음을 시도해 보세요.

path = r'%s' % os.getcwd().replace('\\','/')

print(__file__)
print(__import__("pathlib").Path(__file__).parent)

나는 __file__과 함께 접근 방식을 사용했습니다.
os.path.abspath(__file__)
그러나 약간의 속임수가 있습니다. 코드가 처음 실행될 때 .py 파일을 반환하고 다음 실행은 *.pyc 파일의 이름을 부여합니다.
그래서 나는 다음과 함께 머물렀다:
inspect.getfile(inspect.currentframe())
또는
sys._getframe().f_code.co_filename

Eclipse를 고려한 함수를 작성했습니다. 디버거 그리고 단위 테스트.실행한 첫 번째 스크립트의 폴더를 반환합니다.선택적으로 다음을 지정할 수 있습니다. __파일__ var이지만 가장 중요한 점은 이 변수를 모든 프로젝트에서 공유할 필요가 없다는 것입니다. 호출 계층 구조.

어쩌면 내가 보지 못한 다른 스택의 특정 사례를 처리할 수도 있지만 나에게는 괜찮습니다.

import inspect, os
def getRootDirectory(_file_=None):
    """
    Get the directory of the root execution file
    Can help: http://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing
    For eclipse user with unittest or debugger, the function search for the correct folder in the stack
    You can pass __file__ (with 4 underscores) if you want the caller directory
    """
    # If we don't have the __file__ :
    if _file_ is None:
        # We get the last :
        rootFile = inspect.stack()[-1][1]
        folder = os.path.abspath(rootFile)
        # If we use unittest :
        if ("/pysrc" in folder) & ("org.python.pydev" in folder):
            previous = None
            # We search from left to right the case.py :
            for el in inspect.stack():
                currentFile = os.path.abspath(el[1])
                if ("unittest/case.py" in currentFile) | ("org.python.pydev" in currentFile):
                    break
                previous = currentFile
            folder = previous
        # We return the folder :
        return os.path.dirname(folder)
    else:
        # We return the folder according to specified __file__ :
        return os.path.dirname(os.path.realpath(_file_))

가장 간단한 방법은 다음과 같습니다.

~에 script_1.py:

import subprocess
subprocess.call(['python3',<path_to_script_2.py>])

~에 script_2.py:

sys.argv[0]

추신.:난 노력 했어 execfile, 하지만 script_2.py를 문자열로 읽기 때문에, sys.argv[0] 반환 <string>.

저는 항상 Current Working Directory(CWD)의 os 기능을 사용해 왔습니다.이는 표준 라이브러리의 일부이며 구현하기가 매우 쉽습니다.예는 다음과 같습니다.

    import os
    base_directory = os.getcwd()

이러한 답변의 대부분은 Python 버전 2.x 이하로 작성되었습니다.Python 3.x에서는 인쇄 함수의 구문이 괄호를 요구하도록 변경되었습니다.인쇄().

따라서 Python 2.x에서 user13993의 이전 최고 점수 답변은 다음과 같습니다.

import inspect, os
print inspect.getfile(inspect.currentframe()) # script filename (usually with path)
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory

Python 3.x에서는 다음과 같습니다.

import inspect, os
print(inspect.getfile(inspect.currentframe())) # script filename (usually with path)
print(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) ) # script directory

없이 파일 이름만 원하는 경우 ./ 또는 .py 당신은 이것을 시도 할 수 있습니다

filename = testscript.py
file_name = __file__[2:-3]

file_name 테스트 스크립트를 인쇄합니다.

import os

import wx


# return the full path of this file
print(os.getcwd())

icon = wx.Icon(os.getcwd() + '/img/image.png', wx.BITMAP_TYPE_PNG, 16, 16)

# put the icon on the frame
self.SetIcon(icon)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top