轻松找到下一个可用文件名
NextFreeFileName的Python项目详细描述
nextFreeFileName
nextfreefilename是一个微包,旨在简化查找下一个可用文件名的过程 作为大型进程的一部分生成文件时。
概述
要了解nextfreefilename的用途,请仔细阅读一个示例案例。
假设以下情况:
- 您正在使用多个json文件的内容生成svg文件
- 一个处理过的json文件的文件名是:
performance_graph.json
- 您需要使用此json中的信息生成3个svg文件
- 您希望所有生成的svg共享一个通用名称(通常是源文件的名称)
- 您希望通过在生成的svg的文件名中添加数字后缀来区分它们
要查找下一个可用文件名,可以执行以下语句:
fromNextFreeFileNameimportNextFreeFileNamesrc_json='/tmp/performance_graph.json'svg_out=NextFreeFileName('/tmp/performance_graph.svg')
如果^ {< CD2>}已经存在,^ {< CD3>}返回下面的^ {< CD4>}对象:PosixPath('/tmp/performance_graph_0.svg')
否则,返回的原始路径没有数字后缀:PosixPath('/tmp/performance_graph.svg')
。
可以使用NextFreeFileName.next()
方法动态生成文件名。
演示
为了演示真实的用例,让我们生成4个空的.txt文件:
fromNextFreeFileNameimportNextFreeFileNameout_txt=NextFreeFileName('/tmp/performance_graph.txt')# Can be str OR class 'pathlib.*'i=0whilei<4:withopen(out_txt.next(),'w')asf:f.write('Some text...')f.close()i+=1
结果:
ls
# performance_graph.txt performance_graph_0.txt performance_graph_1.txt performance_graph_2.txt
API说明
classNextFreeFileName:def__init__(self,file_path,glue="_",index=0,ignore_reserved=False,resolve_suffix=True,**kwargs):""" Takes a path to a file and if already exists, a number gets appended to its end. If file exists, glue + index gets appended to it. If file does not exist, the initial value of file_path gets returned as Path. :param file_path: (str | Path) Path to existing or desired output :param glue: (str) Character gluing together the `original file name` and the `index`. Default=_ (underscore) :param index: (int) Number to start counting from :param ignore_reserved: Set to True if you don't want to compare glue's value to reserved character's list :param resolve_suffix: Sets index based on the file name's current numeric suffix (if exists) :keyword reserved_characters: (str[]): List of strings added to Reserved Characters """
- 要初始化
NextFreeFileName
,必须为file_path
提供一个值。
接受的类型:str或类似pathlib的对象。 - 默认情况下,文件名和生成的整数用下划线(
_
)粘合在一起。
要重写它,请将glue
的值设置为任何有效的Unicode字符。异常:* / : < > ? \ | "
。 - 默认情况下,生成的整数从
0
开始。
要重写它,请将index
的值设置为任何有效整数。 - 默认情况下,
glue
限制几个字符(见上文)。
要禁用此限制,请将ignore_reserved
设置为True
。
保留字符
File systems have not always provided the same character set for composing a filename. Before Unicode became a de facto standard, file systems mostly used a locale-dependent character set. By contrast, some new systems permit a filename to be composed of almost any character of the Unicode repertoire, and even some non-Unicode byte sequences. Limitations may be imposed by the file system, operating system, application, or requirements for interoperability with other systems.
Many file system utilities prohibit control characters from appearing in filenames. In Unix-like file systems, the null character and the path separator / are prohibited.
File system utilities and naming conventions on Windows prohibit particular characters from appearing in filenames.
为了实现高度的跨平台兼容性,默认情况下会限制以下字符:
@propertydef_reserved_characters(self):return['*',# asterisk/star'/',# slash':',# colon'<',# less than'>',# greater than'?',# question mark'\\',# backslash'|',# vertical bar/pipe'"',# quote*self._custom_reserved_characters]
参数
- 若要扩展保留字符列表,请在调用
NextFreeFileName
时使用reserved_characters
kwarg。reserved_characters
必须是Kwarg类型的字符串列表。 - 要禁用保留字符检查,请在调用
NextFreeFileName
时将ignore_reserved
设置为False
。
安装
要安装nffn,请执行以下步骤:
- 获取最新的Release或克隆存储库:
git clone git@github.com:theriverman/NextFreeFileName.git
- 通过执行以下命令安装到virtualenv:
python setup.py install
- 将主类导入项目:
from NFFN import NextFreeFileName
稍后可能会添加pypi包。
贡献/包装
若要参与,请分叉存储库,提交更改并打开拉取请求。
这个包在PYPI有售。
要通过pypi发布新包,请发出以下命令:
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
兼容性
Python3.6.0及以上版本。
这个库利用f字符串PEP 498和 键入提示PEP 484。
版权所有
麻省理工学院许可证