windows 需要一种方法来确定文件是否已完成写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3070210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:41:01  来源:igfitidea点击:

Need a way to determine if a file is done being written to

pythonwindowslinuxpdffile-io

提问by Khorkrak

The situation I'm in is this - there's a process that's writing to a file, sometimes the file is rather large say 400 - 500MB. I need to know when it's done writing. How can I determine this? If I look in the directory I'll see it there but it might not be done being written. Plus this needs to be done remotely - as in on the same internal LAN but not on the same computer and typically the process that wants to know when the file writing is done is running on a Linux box with a the process that's writing the file and the file itself on a windows box. No samba isn't an option. xmlrpc communication to a service on that windows box is an option as well as using snmp to check if that's viable.

我的情况是这样的 - 有一个进程正在写入文件,有时文件相当大,比如 400 - 500MB。我需要知道什么时候写完。我如何确定这一点?如果我查看目录,我会在那里看到它,但它可能不会被写入。此外,这需要远程完成 - 就像在同一个内部 LAN 上但不在同一台计算机上一样,通常想要知道文件写入何时完成的进程正在 Linux 机器上运行,并且该进程正在写入文件和文件本身在 Windows 框中。没有桑巴不是一种选择。与该 Windows 框上的服务进行 xmlrpc 通信是一种选择,也可以使用 snmp 来检查这是否可行。

Ideally

理想情况下

  • Works on either Linux or Windows - meaning the solution is OS independent.
  • Works for any type of file.
  • 适用于 Linux 或 Windows - 这意味着该解决方案独立于操作系统。
  • 适用于任何类型的文件。

Good enough:

够好了:

  • Works just on windows but can be done through some library or whatever that can be accessed with Python.
  • Works only for PDF files.
  • 仅在 Windows 上工作,但可以通过一些库或任何可以用 Python 访问的东西来完成。
  • 仅适用于 PDF 文件。

Current best idea is to periodically open the file in question from some process on the windows box and look at the last bytes checking for the PDF end tag and accounting for the eol differences because the file may have been created on Linux or Windows.

当前最好的想法是定期从 windows 框上的某个进程打开有问题的文件,并查看最后一个字节,检查 PDF 结束标记并说明 eol 差异,因为该文件可能是在 Linux 或 Windows 上创建的。

采纳答案by Khorkrak

I ended up resolving it for our situation. As it turns out the process that was writing the files out had them opened exclusively so all we had to do was try opening them for read access - when denied they were in use.

我最终解决了我们的情况。事实证明,写出文件的过程以独占方式打开了它们,所以我们所要做的就是尝试打开它们以进行读取访问 - 当拒绝它们正在使用时。

回答by Jeroen Ritmeijer

There are probably many approaches you can take. I would try to open the file with write access. If that succeeds then no-one else is writing to that file.

可能有很多方法可以采用。我会尝试打开具有写访问权限的文件。如果成功,则没有其他人正在写入该文件。

Build a web service around this concept if you don't have direct access to the file between machines.

如果您无法直接访问机器之间的文件,请围绕此概念构建 Web 服务。