Check absolute paths in Python?

$ touch foo $ ln -s foo bar $ python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) GCC 4.0.1 (Apple Inc. Build 5465) on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> help(os.path.

Samefile) Help on function samefile in module posixpath: samefile(f1, f2) Test whether two pathnames reference the same actual file >>> os.path. Samefile("foo", "bar") True.

Nice answer. I didn not know about this method! – jkp Jul 30 '09 at 17:12 Note that this is documented as only available on Unix.It is not present on Windows, at least in Python 2.6. – Weeble Jul 5 '10 at 10:31.

You want to use os.path. Abspath(path) to normalize each path for comparison. Os.path.

Abspath(foo) == os.path. Abspath(bar).

A simple string compare should work: import os print os.path. Abspath(first) == os.path. Abspath(second) Credit to Andrew, he corrected my initial post which included a call to os.path.

Normpath: this is unneeded because the implementation of os.path. Abspath does it for you.

You don't need to call normpath first. – Andrew Hare Jul 30 '09 at 17:10 Andrew: if you were just going to compare the strings you may end up with different paths pointing to the same file in some cases. Os.

Abspath doesn't do anything clever, it only adds a prefix. – jkp Jul 30 '09 at 17:11 @Andrew: no I take it back! You were right, for *nix at least the implementation does that for you.

Every day is a schoolday :) – jkp Jul 30 '09 at 17:15 I disagree, from docs.python. Org/library/os.path. Html: Return a normalized absolutized version of the pathname path.

On most platforms, this is equivalent to normpath(join(os.getcwd(), path)). – Andrew Hare Jul 30 '09 at 17:16 @jkp - Haha! No worries - I live and learn as well!

:) – Andrew Hare Jul 30 '09 at 17:17.

On Windows systems, there is no samefile function and you also have to worry about case. The normcase function from os. Path can be combined with abspath to handle this case.

From os. Path import abspath, normcase def are_paths_equivalent(path1, path2): return normcase(abspath(path1)) == normcase(abspath(path2)) This will consider "C:\SPAM\Eggs. Txt" to be equivalent to "c:\spam\eggs.

Txt" on Windows. Note that unlike samefile, all methods based on normalizing and comparing paths will not be aware of cases where completely different paths refer to the same file. On Windows, this means that if you use SUBST, MKLINK or mounted network shares to create multiple distinct paths to the same file, none of these solutions will be able to say "that's the same file".

Hopefully that's not too much of a problem most of the time.

May be one can use os.path. Relpath(path1, path2) as workaround for os.path. Samefile(path1, path2) on Windows?

If os.path. Relpath(path1, path2) returns '. ' than path1 and path2 point to the same place.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions