os.path.basename()과 os.path.dirname()의 차이점은 무엇입니까?
사이의 차이점은 무엇입니까?os.path.basename()
그리고.os.path.dirname()
?
나는 이미 답을 검색하고 링크를 읽었지만 이해하지 못했습니다.누구든지 간단한 설명을 해줄 수 있습니까?
두 기능 모두 다음 기능을 사용합니다.os.path.split(path)
경로 이름을 분할하는 함수path
한 쌍으로;(head, tail)
.
그os.path.dirname(path)
function은 경로의 머리를 반환합니다.
예: 의 dirname'/foo/bar/item'
이라'/foo/bar'
.
그os.path.basename(path)
function은 경로의 테일을 반환합니다.
예: 의 기본 이름'/foo/bar/item'
돌아온다'item'
보낸 사람: http://docs.python.org/3/library/os.path.html#os.path.basename
위에서 Breno가 언급한 내용을 요약하면,
파일에 대한 경로가 있는 변수가 있다고 가정합니다.
path = '/home/User/Desktop/myfile.py'
os.path.basename(path)
문자열을 반환합니다.'myfile.py'
그리고.
os.path.dirname(path)
문자열을 반환합니다.'/home/User/Desktop'
('/' 후행 슬래시 없음)
이러한 함수는 전체 경로 이름이 지정된 파일 이름/디렉토리 이름을 가져와야 할 때 사용됩니다.
파일 경로가 파일 이름일 경우(예: 파일 이름 대신path = '/home/User/Desktop/myfile.py'
당신은 방금myfile.py
),os.path.dirname(path)
빈 문자열을 반환합니다.
언급URL : https://stackoverflow.com/questions/22272003/what-is-the-difference-between-os-path-basename-and-os-path-dirname
'programing' 카테고리의 다른 글
스프링 보안에 대한 요청 Matchers() 이해 (0) | 2023.06.23 |
---|---|
traceback / sys.exc_info() 값을 변수에 저장하는 방법은 무엇입니까? (0) | 2023.06.23 |
%23이 들어 있는 URL을 다시 쓰는 방법은 무엇입니까? (0) | 2023.06.18 |
매크로 - 폴더의 모든 파일 열기 (0) | 2023.06.18 |
Ruby 어레이: %w 대 %W (0) | 2023.06.18 |