Welcome, guest | Sign In | My Account | Store | Cart

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

Python, 22 lines
 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

4 comments

Gabriel Genellina 12 years, 3 months ago  # | flag

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.

Denis Barmenkov (author) 12 years, 2 months ago  # | flag

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 ;)

Denis Barmenkov (author) 12 years, 2 months ago  # | flag

Recipe renamed.

Created by Denis Barmenkov on Wed, 19 Aug 2009 (MIT)
Python recipes (4591)
Denis Barmenkov's recipes (20)

Required Modules

  • (none specified)

Other Information and Tasks