简体   繁体   English

Python - 如果链接存在,则覆盖它

[英]Python - overwrite a link if it exists

from os import link

link('WInd_Rose_Aguiar.svg', 'Wikipedia Daily Featured Picture')

# A day has passed

link('Piero_del_Pollaiuolo_-_Profile_Portrait_of_a_Young_Lady_-_Gemäldegalerie_Berlin_-_Google_Art_Project.jpg',
     'Wikipedia Daily Featured Picture') # Exception

The results of calling the above script:调用上述脚本的结果:

my@comp:~/wtfdir$ python3 wtf.py
Traceback (most recent call last):
  File "wtf.py", line 8, in <module>
    'Wikipedia Daily Featured Picture') # Exception
FileExistsError: [Errno 17] File exists: 'Piero_del_Pollaiuolo_-_Profile_Portrait_of_a_Young_Lady_-_Gemäldegalerie_Berlin_-_Google_Art_Project.jpg' -> 'Wikipedia Daily Featured Picture'

Creating the first link succeeds.创建第一个链接成功。 Creating the second one fails.创建第二个失败。

That's hardly what I would expect… My intention is to overwrite this link.这几乎不是我所期望的……我的意图是覆盖这个链接。

https://docs.python.org/3/library/os.html#os.link ⇐ I can't see a force or overwrite_if_exists or similar parameter to the function link in the docs. https://docs.python.org/3/library/os.html#os.link ⇐ 我看不到 ZC1C425268E68385D11Z5074 中的 ZC1C425268E68385D11Z5074 linkforceoverwrite_if_exists或类似参数

How can I create a link pointing to a new source, overwriting the previous link if it exists?如何创建指向新源的链接,如果存在则覆盖以前的链接?

Well yes – I guess I can do sth like this:嗯,是的——我想我可以这样做:

from os import link, remove
from os.path import isfile

def force_link(src, dest):
    if isfile(dest):
        remove(dest)
    link(src, dest)

force_link('WInd_Rose_Aguiar.svg', 'Wikipedia Daily Featured Picture')

# A day has passed

force_link('Piero_del_Pollaiuolo_-_Profile_Portrait_of_a_Young_Lady_-_Gemäldegalerie_Berlin_-_Google_Art_Project.jpg',
     'Wikipedia Daily Featured Picture') # No longer exception

But this is cumbersome and at least in theory may be incorrect (what if some other process re-creates the file between remove(dest) and link(src, dest) ?).但这很麻烦,至少在理论上可能是不正确的(如果其他进程在remove(dest)link(src, dest)之间重新创建文件怎么办?)。 And while perhaps this (at least theoretical) incorrectness could be resolved, the resulting code would be even more cumbersome, I guess…虽然也许可以解决这个(至少理论上的)不正确性,但生成的代码会更加麻烦,我猜......

There must be a better, more right-handed way to do this!必须有更好,更右手的方法来做到这一点!

Create a new link for the file you want to expose.为要公开的文件创建一个新链接。 Then replace your fixed link with the new link you just created.然后用您刚刚创建的新链接替换您的固定链接。

from tempfile import TemporaryDirectory


def force_link(src, dest):
    with TemporaryDirectory(dir=os.path.dirname(dest)) as d:
        tmpname = os.path.join(d, "foo")
        os.link(src, tmpname)
        os.replace(tmpname, dest)

You may need to ensure that the permissions on dest are correct afterwards.之后您可能需要确保对dest的权限是正确的。

os.link will succeed in securely creating a new link in the temporary directory. os.link将成功在临时目录中安全地创建一个新链接。 Then you'll use os.replace to securely rename the temporary link to dest , effectively overwriting the old link with the new.然后,您将使用os.replace将临时链接安全地重命名为dest ,从而有效地用新链接覆盖旧链接。

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

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