ActiveState Code

Recipe 576886: simple samefile() for windows (local drive case only)


A simple replacement of the os.path.samefile() function not existing on the Windows platform. MAC/Unix supported in standard way :).

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def samefile(f1, f2):
    '''
    A simple replacement of the os.path.samefile() function not existing 
    on the Windows platform. 
    MAC/Unix supported in standard way :).

    Author: Denis Barmenkov <denis.barmenkov@gmail.com>

    Source: code.activestate.com/recipes/576886/

    Copyright: this code is free, but if you want to use it, please
               keep this multiline comment along with function source. 
               Thank you.

    2009-08-19 20:13 
    '''
    try:
        return os.path.samefile(f1, f2)
    except AttributeError:
        f1 = os.path.abspath(f1).lower()
        f2 = os.path.abspath(f2).lower()
        return f1 == f2

Comments

  1. 1. At 7:35 p.m. on 20 aug 2009, Gabriel Genellina said:

    It simply doesn't work, except on the most simple cases.

    What about SUBSTed drives? Junction points? Symbolic links? Network shares? Neither of them are resolved by a simple abspath call.

    Sorry but a "true" samefile replacement will take much more than that.

  2. 2. At 2:25 a.m. on 9 sep 2009, Denis Barmenkov (the author) said:

    Its not a 'all ways solution' but a simple example for testing two files on local drive.

    Windows extra entities are too complicated to work with ;)

  3. 3. At 2:27 a.m. on 9 sep 2009, Denis Barmenkov (the author) said:

    Recipe renamed.

  4. 4. At 3:54 a.m. on 4 oct 2009, Denis Barmenkov (the author) said:

Sign in to comment