[Python] 파이썬 경로 결합하기 join()
구문
import os.path
os.path.join(경로1, 경로2, ...)
설명
이 함수는 인수들을 받아 결합하여 경로를 만들고 리턴합니다.
함수에 인수 넣은 순서대로 경로가 만들어집니다.
UNIX 계열을 사용하는 개발자는 인수에 슬래시(/) 넣는 것을 조심해야 합니다.
os.path.join('.', 'test1', '/test2') 의 경우,
시스템은 슬래시(/)를 보고 루트 디렉터리로부터 경로라고 간주하여
다른 모든 인수를 무시하고, /test2 만 결과로 출력합니다.
예시
import os.path
result = os.path.join('.', 'test1', 'test2')
print(result)
# 출력 결과--> .\test1\test2
# 출력 결과는 처리 시스템에 따라 다릅니다.
참조
파이썬 도큐먼트
https://docs.python.org/3/library/os.path.html
os.path — Common pathname manipulations
Source code: Lib/posixpath.py(for POSIX) and Lib/ntpath.py(for Windows). This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesyst...
docs.python.org
os.path.join(path, *paths)
Join one or more path segments intelligently. The return value is the concatenation of path and all members of *paths, with exactly one directory separator following each non-empty part, except the last. That is, the result will only end in a separator if the last part is either empty or ends in a separator. If a segment is an absolute path (which on Windows requires both a drive and a root), then all previous segments are ignored and joining continues from the absolute path segment.
On Windows, the drive is not reset when a rooted path segment (e.g., r'\foo') is encountered. If a segment is on a different drive or is an absolute path, all previous segments are ignored and the drive is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
Changed in version 3.6: Accepts a path-like object for path and paths.