一种在不覆盖的情况下创建文件和目录的方法

2024-04-27 04:38:26 发布

您现在位置:Python中文网/ 问答频道 /正文

你知道当你下载的东西和下载文件夹包含一个同名的文件,而不是覆盖它或抛出一个错误,该文件的结尾附加了一个数字?例如,如果我想下载my_file.txt,但它已经存在于目标文件夹中,那么新文件将命名为my_file(2).txt。如果我再试一次,就会是my_file(3).txt。你知道吗

我想知道在python3.x中是否有一种方法来检查这个问题并获得一个唯一的名称(不一定要创建文件或目录)。我目前正在实现它:

import os
def new_name(name, newseparator='_')
    #name can be either a file or directory name

    base, extension = os.path.splitext(name)
    i = 2
    while os.path.exists(name):
        name = base + newseparator + str(i) + extension
        i += 1

    return name

在上面的示例中,如果cwd中已经存在my_file.txt,则运行new_file('my_file.txt')将返回my_file_2.txtname还可以包含完整路径或相对路径,它也可以工作。你知道吗


Tags: 文件pathnametxt文件夹newbaseos
2条回答

我会使用PathLib并按照以下思路做一些事情:

from pathlib import Path 

def new_fn(fn, sep='_'):
    p=Path(fn)
    if p.exists():
        if not p.is_file(): 
            raise TypeError
        np=p.resolve(strict=True)
        parent=str(np.parent)
        extens=''.join(np.suffixes)  # handle multiple ext such as .tar.gz
        base=str(np.name).replace(extens,'')
        i=2
        nf=parent+base+sep+str(i)+extens    
        while Path(nf).exists():
            i+=1
            nf=parent+base+sep+str(i)+extens    
        return nf   
    else:       
        return p.parent.resolve(strict=True) / p 

这只处理写入的文件,但处理目录(稍后添加)的方法相同。我将把它作为一个项目留给读者。你知道吗

另一种获取新名称的方法是使用内置的tempfile模块:

from pathlib import Path
from tempfile import NamedTemporaryFile

def new_path(path: Path, new_separator='_'):
    prefix = str(path.stem) + new_separator
    dir = path.parent
    suffix = ''.join(path.suffixes)

    with NamedTemporaryFile(prefix=prefix, suffix=suffix, delete=False, dir=dir) as f:
        return f.name

如果从下载目录中执行此函数,您将得到如下结果:

>>> new_path(Path('my_file.txt'))
'/home/krassowski/Downloads/my_file_90_lv301.txt'

其中90_lv301部分是由Python的tempfile模块在内部生成的。你知道吗

注意:使用delete=False参数,函数将用新名称创建(并保留未删除)一个空文件。如果您不想用这种方式创建一个空文件,只需删除delete=False,但是保留它会阻止其他人在您下一次操作之前创建一个具有此名称的新文件(尽管他们仍然可以覆盖它)。你知道吗

简单地说,如果您(或最终用户)要同时运行两次程序,使用delete=False可以防止并发问题。你知道吗

相关问题 更多 >