简体   繁体   English

在Python中使用os.rename()而不是os.remove()删除文件是否明智?

[英]Is it wise to remove files using os.rename() instead of os.remove() in Python?

I have a file named fin that I need to modify. 我有一个名为fin的文件,需要修改。

In Python I created a script that makes the modifications I need and then save it in new file named fout . 在Python中,我创建了一个脚本,进行所需的修改,然后将其保存在名为fout新文件中。

The older file is useless to me, so I use os.remove('fin') to remove it. 较旧的文件对我无用,因此我使用os.remove('fin')删除了它。 However, the modified file should be named fin , so I use os.rename('fout','fin') . 但是,修改后的文件应命名为fin ,因此我使用os.rename('fout','fin')

I thought about a shortcut that is straight up using os.rename('fout','fin') , expecting it to delete fin since it's the same name, but I`m not sure if this is in fact deleting my older file or if it may cause some trouble if I do this several times (doing this task over a 1000 times). 我想到了一个使用os.rename('fout','fin')的快捷方式,因为它是同一个名称,所以期望它删除fin ,但是我不确定这是否实际上是在删除旧文件或如果多次执行此操作可能会造成一些麻烦(执行此任务超过1000次)。

My question is: is this the cleanest and fastest way to achieve this goal? 我的问题是:这是实现这一目标的最干净,最快的方法吗? In summary I just want to make corrections in the original file and overwriting it. 总之,我只想在原始文件中进行更正并覆盖它。

code: 码:

import os

f = open('fin','w') 
f.write('apples apple apples\napple apples apple\napples apple apples') 
f.close()

with open('fin', 'rt') as fin:
   with open('fout', 'wt') as fout:
      for line in fin:
         fout.write(line.replace('apples', 'orange'))
os.rename('fout', 'fin')

Your pattern works on POSIX systems, but won't work on Windows. 您的模式可以在POSIX系统上使用,但不能在Windows上使用。 I'd recommend using os.replace which replaces existing files in an OS agnostic fashion. 我建议使用os.replace以OS不可知的方式替换现有文件。 It requires Python 3.3 or higher, but then, new code should generally be targetting Python 3 anyway. 它要求使用Python 3.3或更高版本,但是无论如何,新代码通常应该以Python 3为目标。

It's the preferred pattern because it guarantees atomicity; 这是首选模式,因为它可以保证原子性。 the fin file is always in a complete state, either before or after, with no risk of failures leading to it existing in a partial/corrupt state. fin文件之前或之后始终处于完整状态,没有失败的风险,导致文件处于部分/损坏状态。

It doesn't seem from your example that you would have a reason to iterate through the file line by line. 从您的示例看来,您似乎没有理由逐行遍历文件。 If that is the case this is a pretty simple solution. 如果是这样,这是一个非常简单的解决方案。

f = open('fin','w').write('apples apple apples\napple apples apple\napples apple apples')

s = open('fin').read().replace('apples', 'oranges')
open('fin','w').write(s)

For the sake of cross compatibilty, I recommend you invert the with code. 为了实现交叉兼容性,我建议您将with代码反转。 My suggestion code is 我的建议代码是

f = open('fin','w') 
f.write('apples apple apples\napple apples apple\napples apple apples') 
f.close()

with open('fout', 'wt') as fout:
    with open('fin', 'rt') as fin:
        for line in fin.readlines():
            fout.write(line.replace('apples', 'orange'))
    os.unlink('fin')
    os.rename('fout', 'fin')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM