如何在Windows和Python 2.7上模拟os.path.samefile行为? [英] How to emulate os.path.samefile behaviour on Windows and Python 2.7?
问题描述
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屋!