如何在Windows和Python 2.7上模拟os.path.samefile行为? [英] How to emulate os.path.samefile behaviour on Windows and Python 2.7?

查看:536
本文介绍了如何在Windows和Python 2.7上模拟os.path.samefile行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个路径,如果它们指向相同的文件,我必须进行比较。在Unix中,这可以使用 os.path.samefile 来完成,但是作为文档指出它在Windows中不可用。
模拟此功能的最佳方法是什么?
它不需要模拟常见的情况。在我的情况下,有以下简化:
$ b $ ul
  • 路径不包含符号链接。



  • 现在我使用以下内容:


    pre> def samefile(path1,path2)
    return os.path.normcase(os.path.normpath(path1))== \
    os.path。 normcase(os.path.normpath(path2))

    是这样吗?

    解决方案

    根据 issue#5985 os.path.samefile和os.path.sameopenfile现在在py3k中。我在Python 3.3.0上对此进行了验证。



    对于老版本的Python,这里使用 GetFileInformationByHandle 函数:

    <
    a href =http://timgolden.me.uk/python/win32_how_do_i/see_if_two_files_are_the_same_file.html =nofollow> see_if_two_files_are_the_same_file

    Given two paths I have to compare if they're pointing to the same file or not. In Unix this can be done with os.path.samefile, but as documentation states it's not available in Windows. What's the best way to emulate this function? It doesn't need to emulate common case. In my case there are the following simplifications:

    • Paths don't contain symbolic links.
    • Files are in the same local disk.

    Now I use the following:

    def samefile(path1, path2)
        return os.path.normcase(os.path.normpath(path1)) == \
               os.path.normcase(os.path.normpath(path2))
    

    Is this OK?

    解决方案

    According to issue#5985 the os.path.samefile and os.path.sameopenfile are now in py3k. I verified this on Python 3.3.0

    For older versions of Python here's a way which uses the GetFileInformationByHandle function:

    see_if_two_files_are_the_same_file

    这篇关于如何在Windows和Python 2.7上模拟os.path.samefile行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆