[ACCEPTED]-In Python 3.4, what is best/easiest way to compare paths?-pathlib

Accepted answer
Score: 10

To compare you should resolve the paths first or 3 you can also use os.path.samefile. Example:

print(path1.resolve() == path2.resolve())
# True       

import os
print(os.path.samefile(str(path1), str(path2)))
# True

By the way, path1 is path2 checkes 2 if path1 is the same object as path2 rather than comparing 1 actual paths.

Score: 3

For anyone using a newer python version 2 than OP: Starting with python 3.5, you can 1 also use path1.samefile(path2), see documentation.

More Related questions