判断是否为同一文件

方法:通过文件够本的Volume Serial Number和Index Number来实现。
注意:当关闭一个文件,再打开另一个与之完全不相关的文件时,这个文件的Index Number可能与前一个完全相同,可以同时打开两个需要比较的文件,在未进行比较完成前,不要调用hHandle.Close()
import os, sys
import tempfile
import win32file
def get_read_handle (filename):
  return win32file.CreateFile (
    filename,
    win32file.GENERIC_READ,
    win32file.FILE_SHARE_READ,
    None,
    win32file.OPEN_EXISTING,
    0,
    None
  )
def get_unique_id (hFile):
  (
    attributes,
    created_at, accessed_at, written_at,
    volume,
    file_hi, file_lo,
    n_links,
    index_hi, index_lo
  ) = win32file.GetFileInformationByHandle (hFile)
  return volume, index_hi, index_lo
def files_are_equal (filename1, filename2):
  hFile1 = get_read_handle (filename1)
  hFile2 = get_read_handle (filename2)
  are_equal = (get_unique_id (hFile1) == get_unique_id (hFile2))
  hFile2.Close ()
  hFile1.Close ()
  return are_equal
#
# This bit of the example will only work on Win2k+; it
#  was the only way I could reasonably produce two different
#  files which were the same file, without knowing anything
#  about your drives, network etc.
#
filename1 = sys.executable
filename2 = tempfile.mktemp (".exe")
win32file.CreateHardLink (filename2, filename1, None)
print filename1, filename2, files_are_equal (filename1, filename2)