파일과 디렉터리를 식별하는 /home/dinsdale 같은 문자열을 경로(path)라고 한다.
memo.txt 같은 간단한 파일 이름도 경로라고 하지만, 현재 디렉터리가 필요하므로 상대 경로(relative path)라고 한다. 현재 디렉터리가 /home/dinsdale이면 파일 이름 memo.txt는 /home/dinsdale/memo.txt로 참조할 수 있다.
/로 시작하는 경로는 현재 디렉터리가 필요하지 않다. 그래서 이를 절대 경로(absolute path)라고 한다. 파일의 절대 경로를 알아내려면 os.path.abspath를 사용할 수 있다.
>>> os.path.abspath('memo.txt')
'/home/dinsdale/memo.txt'
os.path에는 파일 이름과 경로에 대해 사용할 수 있는 다른 함수들도 제공된다. 예를 들어 os.path.exists는 파일이나 디렉터리가 있는지 검사한다.
>>> os.path.exists('memo.txt')
True
경로가 있다면 os.path.isdir로 경로가 디렉터리인지 검사한다.
>>> os.path.isdir('memo.txt')
False
>>> os.path.isdir('/home/dinsdale')
True