免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1821 | 回复: 0
打印 上一主题 下一主题

判断是否为同一文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-27 23:31 |只看该作者 |倒序浏览
方法:通过文件够本的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)


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19742/showart_369153.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP