OS6 Python 현재 디렉토리와 파일의 디렉토리를 찾으세요 [중복], Find the current directory and file's direc 질문How do I determine: the current directory (where I was in the shell when I ran the Python script), and where the Python file I am executing is? 답변파이썬 파일이 포함된 디렉토리의 전체 경로를 얻으려면 해당 파일에 다음을 작성하십시오:import os dir_path = os.path.dirname(os.path.realpath(__file__)) (위의 주문은 현재 작업 디렉토리를 변경하기 위해 os.chdir()를 이미 사용한 경우 작동하지 않습니다. __file__ 상수의 값은 현재 작업 디렉토리를 기준으로 상대적이며 os.chdir() 호출에 의해 변경되지 않습니다.)현재 작업 디렉.. 2023. 5. 6. Python 부모 디렉토리가 없으면 디렉토리를 생성하는 방법은 무엇인가요?, How do I create a directory, and any missing parent directories? 질문 주어진 경로에 디렉토리를 만들고 그 경로에 따라 누락된 부모 디렉토리도 만드는 방법은 무엇인가요? 예를 들어, Bash 명령어 mkdir -p /path/to/nested/directory가 이를 수행합니다. 답변 Python ≥ 3.5에서는 pathlib.Path.mkdir를 사용하세요: from pathlib import Path Path("/my/directory").mkdir(parents=True, exist_ok=True) Python의 이전 버전의 경우, 각각 작은 결함이 있는 두 가지 좋은 답변이 있으므로 제 생각을 제시하겠습니다: os.path.exists를 사용하고 os.makedirs를 고려하세요. import os if not os.path.exists(directory): o.. 2023. 5. 4. 이전 1 2 다음