Differences of Path(), Path(__file__) and Path('..') in Python pathlib

Python Path.resolve returns the absolute path of a file/directory.

from pathlib import Path

p1 = Path()
print(p1.resolve())
# /Users/serif/python/test

p2 = Path(__file__)
print(p2.resolve())
# /Users/serif/python/test/zip.py

p3 = Path('.')
print(p3.resolve())
# /Users/serif/python/test

p4 = Path('./')
print(p4.resolve())
# /Users/serif/python/test

p5 = Path('..')
print(p5.resolve())
# /Users/serif/python

p6 = Path('../')
print(p6.resolve())
# /Users/serif/python

p1-p6 are all the PosixPath objects. PosixPath has useful methods and properties and resolve returns the absolute path.

p1 is the current directory and p2 is the current file. p1, p3, p4 are all the same.

  • Path() -> dir
  • Path(file) -> file

Python pathlib is useful for path-handling and almost substitute os.path module.

Python File

Python Tutorial