如何使用Python检查文件保存是否完成?

发布于 2024-09-02 00:03:39 字数 453 浏览 0 评论 0原文

我正在尝试自动化下载过程。在此我想知道特定文件的保存是否完成。场景是这样的。

  1. 使用 Chrome 或 Firefox(任何浏览器)打开站点地址
  2. 使用“Crtl + S”将页面保存到磁盘(我在 Windows 上工作)
  3. 现在,如果页面非常大,则需要几秒钟才能保存。我想在保存完成后解析 html。

由于我无法控制浏览器保存功能,因此我不知道保存是否已完成。

我想到的一个想法是使用 while 循环获取文件的 md5sum,并检查前一个计算的值,然后继续 while 循环,直到前一个和当前的 md5 和匹配。我猜这不起作用,因为浏览器似乎首先尝试将文件保存在 tmp 文件中,然后将内容复制到指定的文件(或者只是重命名文件)。

有什么想法吗?我使用 python 进行自动化,因此任何可以使用 python 实现的想法都是受欢迎的。

谢谢 因德拉吉斯

I am trying to automate a downloading process. In this I want to know, whether a particular file's save is completed or not. The scenario is like this.

  1. Open a site address using either Chrome or Firefox (any browser)
  2. Save the page to disk using 'Crtl + S' (I work on windows)
  3. Now if the page is very big, then it takes few seconds to save. I want to parse the html once the save is complete.

Since I don't have control on the browser save functionality, I don't know whether the save has completed or not.

One idea I thought, is to get the md5sum of the file using a while loop, and check against the previous one calculated, and continue the while loop till the md5 sum from the previous and current one matches. This doesn't works I guess, as it seems browser first attempts to save the file in a tmp file and then copies the content to the specified file (or just renames the file).

Any ideas? I use python for the automation, hence any idea which can be implemented using python is welcome.

Thanks
Indrajith

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2024-09-09 00:03:39

在 Windows 上,您可以尝试以独占访问模式打开文件,以检查它是否正在被其他程序使用(读取或写入)。我用它来等待服务器端完成 FTP 上传,代码如下:

def check_file_ready(self, path):
    '''Check if file is not opened by another process.'''
    handle = None
    try:
        handle = win32file.CreateFile(
            path,
            win32file.GENERIC_WRITE,
            0,
            None,
            win32file.OPEN_EXISTING,
            win32file.FILE_ATTRIBUTE_NORMAL,
            None)
        return True
    except pywintypes.error, e:
        if e[0] == winerror.ERROR_SHARING_VIOLATION:
            # Note: other possible error codes include
            #  winerror.ERROR_FILE_NOT_FOUND
            #  winerror.ERROR_PATH_NOT_FOUND
            #  winerror.ERROR_ACCESS_DENIED.
            return False
        raise
    finally:
        if handle:
            win32file.CloseHandle(handle)

注意:此函数会重新引发除共享冲突之外的所有 win32 错误。您应该事先检查文件是否存在或检查函数中是否有其他错误代码(请参阅第 15 行的注释)。

On Windows you can try to open file in exclusive access mode to check if it's being used (read or written) by some other program. I've used this to wait for complete FTP uploads server-side, here's the code:

def check_file_ready(self, path):
    '''Check if file is not opened by another process.'''
    handle = None
    try:
        handle = win32file.CreateFile(
            path,
            win32file.GENERIC_WRITE,
            0,
            None,
            win32file.OPEN_EXISTING,
            win32file.FILE_ATTRIBUTE_NORMAL,
            None)
        return True
    except pywintypes.error, e:
        if e[0] == winerror.ERROR_SHARING_VIOLATION:
            # Note: other possible error codes include
            #  winerror.ERROR_FILE_NOT_FOUND
            #  winerror.ERROR_PATH_NOT_FOUND
            #  winerror.ERROR_ACCESS_DENIED.
            return False
        raise
    finally:
        if handle:
            win32file.CloseHandle(handle)

Note: this functions re-raises all win32 errors except sharing violation. You should check for file existence beforehead or check for additional error codes in the function (see comment on line 15).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文