使用 Python,如何关闭其他用户通过网络使用的文件?

发布于 2024-12-06 20:20:31 字数 515 浏览 0 评论 0原文

我有一个程序可以创建一堆电影文件。我作为一个 cron 作业运行,每次运行时,上一次迭代中的电影都会移动到“上一个”文件夹,以便始终可以查看以前的版本。

这些电影文件由不同的用户通过网络访问,这就是我遇到问题的地方。

当脚本运行并尝试移动文件时,它会抛出资源繁忙错误,因为文件被不同的用户打开。 Python 有没有办法在我尝试移动这些文件之前强制关闭它们?

进一步澄清:

JMax 提到这是服务器级别问题时是正确的。我可以通过管理工具>访问我们的Windows服务器计算机管理>共享文件夹>打开文件并手动关闭那里的文件,但我想知道是否有Python等效的方法可以达到相同的结果。

像这样的东西:

try:

    shutil.move(src, dst)

except OSError:

  # Close src file on all machines that are currently accessing it and try again.  

I have a program that creates a bunch of movie files. I runs as a cron job and every time it runs the movies from the previous iteration are moved to a 'previous' folder so that there is always a previous version to look at.

These movie files are accessed across a network by various users and that's where I'm running into a problem.

When the script runs and tries to move the files it throws a resource busy error because the files are open by various users. Is there a way in Python to force close these files before I attempt to move them?

Further clarification:

JMax is correct when he mentions it is server level problem. I can access our windows server through Administrative Tools > Computer Management > Shared Folders > Open Files and manually close the files there, but I am wondering whether there is a Python equivalent which will achieve the same result.

something like this:

try:

    shutil.move(src, dst)

except OSError:

  # Close src file on all machines that are currently accessing it and try again.  

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

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

发布评论

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

评论(3

罗罗贝儿 2024-12-13 20:20:31

这个问题与 Python 无关,与您正在使用的特定操作系统和文件系统有关。您能提供这些详细信息吗?

至少在 Windows 中,您可以使用 Sysinternals Handle 强制将特定句柄分配给要关闭的文件。特别是当另一个用户通过网络打开此文件时,此操作非常不稳定,并且可能会导致网络连接随后变得无用。您正在寻找“-c”命令行参数,文档中写道:

关闭指定的句柄(解释为十六进制数)。你
必须通过 PID 指定进程。

警告:关闭句柄可能会导致应用程序或系统不稳定。

如果您在 Linux 中强制关闭通过 Samba 挂载的文件,从经验来看,这是一种徒劳的痛苦经历。然而,其他人的尝试却取得了不同程度的成功。请参阅强制 Samba 进程关闭文件

This question has nothing to do with Python, and everything to do with the particular operating system and file system you're using. Could you please provide these details?

At least in Windows you can use Sysinternals Handle to force a particular handle to a file to be closed. Especially as this file is opened by another user over a network this operation is extremely destabilising and will probably render the network connection subsequently useless. You're looking for the "-c" command-line argument, where the documentation reads:

Closes the specified handle (interpreted as a hexadecimal number). You
must specify the process by its PID.

WARNING: Closing handles can cause application or system instability.

And if you're force-closing a file mounted over Samba in Linux, speaking from experience this is an excruciating experience in futility. However, others have tried with mixed success; see Force a Samba process to close a file.

如梦 2024-12-13 20:20:31

据我所知,您必须结束访问该文件的进程。至少在 Windows 上

As far as I know you have to end the processes which access the file. At least on Windows

夜无邪 2024-12-13 20:20:31

.close() 方法对您的目标文件不起作用?

有关文件对象的更多信息,请参阅深入了解 Python

[编辑]我已重新阅读你的问题。您的问题是用户确实从网络打开同一个文件,而您希望他们关闭该文件?但你能访问他们的操作系统吗?

[EDIT2] 问题更多地出现在服务器级别上,以断开访问该文件的用户的连接。对于 Windows 服务器,请参阅此示例

The .close() method doesn't work on your object file?

See dive into Python for more information on file objects

[EDIT] I've re-read your question. Your problem is that users do open the same file from the network and you want them to close the file? But can you access to their OS?

[EDIT2] The problem is more on a server level to disconnect the user that access the file. See this example for Windows servers.

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