安全地重命名文件而不覆盖 [英] Safely renaming a file without overwriting

查看:89
本文介绍了安全地重命名文件而不覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重命名一个文件,但前提是目标文件名不存在
已经存在。


我可以这样做:<如果os.path.exists(dest):
$跳过文件,引发异常,进行备份...

do_something_else()

else:

os.rename(src,dest)

但是在多用户系统上,有可能dest在检查是否存在和尝试重命名之间的

时间段内创建。


有什么方法可以防止这种情况发生吗?或者我只是试着把支票和

重命名尽可能地靠近,最大限度地减少机会和

希望最好的?

-

史蒂文。

解决方案

Steven D''Aprano写道:
< blockquote class =post_quotes>
我想重命名一个文件,但前提是目标文件名

还不存在。


我可以这样做:


如果os.path.exists(dest):

#skip文件,引发异常,进行备份.. 。

do_something_else()

else:

os.rename(src,dest)


但是在多用户系统上,可能会在检查是否存在和尝试重命名之间的时间段内创建目标。




有什么办法可以防止这种情况发生吗?或者我只是尽量保持

检查并尽可能地重命名,最大限度地减少

的机会并希望最好?



1:用os.open打开文件


2:独占锁定文件 - 其他进程现在无法访问

it。


3:使用重命名重命名文件;这会导致文件系统级别

隐藏旧文件的取消链接(它从文件

系统中消失)但是打开过程仍然可以访问它。


4:关闭文件 - 锁被移除,重命名

敲定。


Wolfgang Draxinger

-

电子邮件地址有效,Jabber:他****** @ jabber。组织,ICQ:134682867

GPG密钥FP:2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E


Wolfgang Draxinger schrieb:


Steven D''Aprano写道:


>我想重命名一个文件,但只是如果目标文件名
尚未存在。

我可以这样做:

如果os.path.exists(dest):
#skip文件,引发异常,进行备份...
do_something_else()
否则:
os.rename(src,dest)

但是在多用户系统上,可能会在检查是否存在和尝试重命名之间的时间段内创建dest。

有没有办法防止这个?或者我只是尽量保持
检查和重命名,尽量减少机会并希望最好?



1:用os.open打开文件


2:独占锁定文件 - 其他进程现在无法访问

it。


3:使用重命名重命名文件;这会导致文件系统级别

隐藏旧文件的取消链接(它从文件

系统中消失)但是打开过程仍然可以访问它。


4:关闭文件 - 锁被删除,重命名

敲定。



这对新文件有帮助吗? OP正确地假设当他测试文件时会发生一个

竞争条件&然后尝试

创建它,因为在此期间它可能已经创建。


Diez


< blockquote> Diez B. Roggisch写道:


> 1:用os.open打开文件
2:独占锁定文件 - 其他进程现在无法访问它。

3:使用重命名重命名文件;这导致文件系统级别隐式取消旧文件的链接(它从
文件系统中消失)但是打开过程仍然可以访问它。

4:关闭文件 - 锁被删除,重命名
最终确定。



使用O_CREAT打开新文件名O_EXCL

标志。对不起,我忘记了明确提到。但是我还没有尝试过它,但它应该可以运行。


Wolfgang Draxinger

-

电子邮件地址有效,Jabber: he******@jabber.org ,ICQ:134682867

GPG密钥FP:2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E


I want to rename a file, but only if the destination file name doesn''t
already exist.

I can do this:

if os.path.exists(dest):
# skip file, raise an exception, make a backup...
do_something_else()
else:
os.rename(src, dest)
But on a multi-user system, it is possible that dest is created in the
time period between checking if it exists and attempting the rename.

Is there any way to prevent this? Or do I just try to keep the check and
the rename as close together as possible, minimizing the chances and
hoping for the best?
--
Steven.

解决方案

Steven D''Aprano wrote:

I want to rename a file, but only if the destination file name
doesn''t already exist.

I can do this:

if os.path.exists(dest):
# skip file, raise an exception, make a backup...
do_something_else()
else:
os.rename(src, dest)
But on a multi-user system, it is possible that dest is created
in the time period between checking if it exists and attempting
the rename.

Is there any way to prevent this? Or do I just try to keep the
check and the rename as close together as possible, minimizing
the chances and hoping for the best?

1: Open the file with os.open

2: Lock the file exclusively -no other process can now access
it.

3: Use rename to rename the file; this causes a file system level
implicit unlink of the old file (it dissappears from the file
system) but the opening process can still access it.

4: close the file -the lock is removed and the rename
finalized.

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867
GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E


Wolfgang Draxinger schrieb:

Steven D''Aprano wrote:

>I want to rename a file, but only if the destination file name
doesn''t already exist.

I can do this:

if os.path.exists(dest):
# skip file, raise an exception, make a backup...
do_something_else()
else:
os.rename(src, dest)
But on a multi-user system, it is possible that dest is created
in the time period between checking if it exists and attempting
the rename.

Is there any way to prevent this? Or do I just try to keep the
check and the rename as close together as possible, minimizing
the chances and hoping for the best?


1: Open the file with os.open

2: Lock the file exclusively -no other process can now access
it.

3: Use rename to rename the file; this causes a file system level
implicit unlink of the old file (it dissappears from the file
system) but the opening process can still access it.

4: close the file -the lock is removed and the rename
finalized.

Where does that help for new files? The OP was right in assuming that a
race condition could occur when he tests for a file & then tries to
create it, as in the meantime it could have been created.

Diez


Diez B. Roggisch wrote:

>1: Open the file with os.open

2: Lock the file exclusively -no other process can now
access it.

3: Use rename to rename the file; this causes a file system
level implicit unlink of the old file (it dissappears from the
file system) but the opening process can still access it.

4: close the file -the lock is removed and the rename
finalized.

The open is to happen on the new file name with O_CREAT | O_EXCL
flags. Sorry, I forgot that to mention explicitly. However I
have not tried it yet, but it should work.

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867
GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E


这篇关于安全地重命名文件而不覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆