一种无需覆盖即可创建文件和目录的方法

时间:2018-11-09 19:41:43

标签: python python-3.x

您知道如何下载内容,并且downloads文件夹中包含一个具有相同名称的文件,而不是覆盖该文件或引发错误,该文件结尾时会附加一个数字吗?例如,如果我要下载my_file.txt,但它已经存在于目标文件夹中,则新文件将被命名为my_file(2).txt。如果我再试一次,它将是my_file(3).txt

我想知道Python 3.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

在上面的示例中,如果new_file('my_file.txt')已在cwd中运行,则运行my_file_2.txt将返回my_file.txtname也可以包含完整或相对路径,也可以使用。

2 个答案:

答案 0 :(得分: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 

这只能处理书面文件,但是相同的方法也可以用于目录(稍后添加)。我将其作为项目留给读者。

答案 1 :(得分:1)

获得新名称的另一种方法是使用内置的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

如果从Downloads目录中执行此功能,则会得到类似以下内容的信息:

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

其中90_lv301部分是由Python的tempfile模块内部生成的。

注意:使用delete=False参数,该函数将创建(并保留未删除的状态)具有新名称的空文件。如果您不希望以这种方式创建一个空文件,只需删除delete=False,但是保留该文件将防止其他人在进行下一个操作之前创建具有该名称的新文件(尽管他们仍然可以覆盖它)

简而言之,如果您(或最终用户)要同时运行两次程序,则拥有delete=False可以防止并发问题。