文件是否已读取或记忆?

2024-06-01 05:06:46 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我正在复制一个文件,然后将其进行比较:

import shutil, filecmp

# dummy file names, they're not important
InFile = "d:\\Some\\Path\\File.ext"
CopyFile = "d:\\Some\\other\\Path\\File_Copy.ext"

# copy the file
shutil.copyfile(InFile,CopyFile)

# compare the two files
if not filecmp.cmp(InFile,CopyFile,shallow=False):
    print "File not copied correctly"

为什么?这似乎没什么意义,不是吗?毕竟我刚刚复制了一个文件,它的大小是一样的,不是吗错了硬盘有一个可接受的错误率,虽然非常小,但仍然存在。唯一可以确定的方法是重新读取文件,但由于它只是在内存中,我如何确定系统(Windows7)实际上从媒体读取了文件,而不是从standby memory返回了页面

假设我必须将16 TB的数据写入可移动硬盘驱动器,并且必须确保光盘上的文件没有损坏,或者至少没有比活动文件更损坏。在16 TB的磁盘空间中,可能有一些文件不完全相同;我目前正在使用WinDiff逐字节检查文件,但是文件比较实用程序很慢,但至少我可以合理地确定,它实际上是在读取从光盘复制的文件,因为页面应该已经消失很久了

有谁能提供一个专家意见,基于肯定性,这是可能发生的:阅读或记住

值得怀疑的是,如果我复制的内存少于安装的内存,那么验证过程会比复制快——应该是,读取比写入快,但不是那么快。如果我复制3GB的文件(我有32GB的内存),这需要一分钟,然后验证应该需要50秒左右,应该是100%的磁盘使用资源监视器。。事实并非如此,验证只需不到10秒,而且资源监视器不会移动。如果我复制的内存多于安装的内存,那么验证所需的时间几乎与安装的一样长,并且资源监视器显示100%——这是我所期望的!这是怎么回事

作为参考,删除了带有错误检查的real代码:

import shutil, filecmp, os, sys

FromFolder = sys.argv[1]
ToFolder   = sys.argv[2]

VerifyList = list()
VerifyToList = list()

BytesCopied = 0

if not os.path.exists(ToFolder):
    os.mkdir(ToFolder)

for (path, dirs, files) in os.walk(FromFolder):
    RelPath = path[len(FromFolder):len(path)]
    OutPath = ToFolder + RelPath

    if not os.path.exists(OutPath):
        os.mkdir(OutPath)

    for thisFile in files:
        InFile = path + "\\" + thisFile
        CopyFile = OutPath + "\\" + thisFile

        ByteSize = os.path.getsize(InFile)
        if ByteSize < 1024:
            RepSize = "%d bytes" % ByteSize
        elif ByteSize < 1048576:
            RepSize = "%.1f KB" %  (ByteSize / 1024) 
        elif ByteSize < 1073741824:
            RepSize = "%.1f MB" %  (ByteSize / 1048576)
        else:
            RepSize = "%.1f GB" %  (ByteSize / 1073741824)

        print "copy %s > %s " % (RepSize, thisFile)

        VerifyList.append(InFile)
        VerifyToList.append(CopyFile)

        shutil.copyfile(InFile,CopyFile)

# finished copying, now verify
FileIndex = range(len(VerifyList))
reVerifyList = list()
reVerifyToList = list()

for thisIndex in FileIndex:
    InFile = VerifyList[thisIndex]
    CopyFile = VerifyToList[thisIndex]

    thisFile = os.path.basename(InFile)
    ByteSize = os.path.getsize(InFile)

    if ByteSize < 1024:
        RepSize = "%d bytes" % ByteSize
    elif ByteSize < 1048576:
        RepSize = "%.1f KB" %  (ByteSize / 1024) 
    elif ByteSize < 1073741824:
        RepSize = "%.1f MB" %  (ByteSize / 1048576)
    else:
        RepSize = "%.1f GB" %  (ByteSize / 1073741824)

    print "Verify %s > %s" % (RepSize, thisFile)

    if not filecmp.cmp(InFile,CopyFile,shallow=False):
        #thisFile = os.path.basename(InFile)
        print "File not copied correctly " + thisFile
        # copy, second chance
        reVerifyList.append(InFile)
        reVerifyToList.append(CopyFile)
        shutil.copyfile(InFile,CopyFile)

del VerifyList
del VerifyToList

if len(reVerifyList) > 0:
    FileIndex = range(len(reVerifyList))
    for thisIndex in FileIndex:
        InFile = reVerifyList[thisIndex]
        CopyFile = reVerifyToList[thisIndex]

        if not filecmp.cmp(InFile,CopyFile,shallow=False):
            thisFile = os.path.basename(InFile)
            print "File failed 2nd chance " + thisFile

Tags: 文件path内存ifosnotinfilefile
1条回答
网友
1楼 · 发布于 2024-06-01 05:06:46

如果使用外部硬盘驱动器,可以关闭此驱动器的写缓存

但你永远不能百分之百肯定,因为一些现代的硬盘有内部缓冲区(SSD)做缓冲透明-现在你的操作系统甚至可以识别它的方式

相关问题 更多 >