轻松找到下一个可用文件名

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.

-- Filename#Reserved characters and words - Wikipedia

为了实现高度的跨平台兼容性,默认情况下会限制以下字符:

@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_characterskwarg。
    reserved_characters必须是Kwarg类型的字符串列表。
  • 要禁用保留字符检查,请在调用NextFreeFileName时将ignore_reserved设置为False

安装

要安装nffn,请执行以下步骤:

  1. 获取最新的Release或克隆存储库:
    git clone git@github.com:theriverman/NextFreeFileName.git
  2. 通过执行以下命令安装到virtualenv:
    python setup.py install
  3. 将主类导入项目:
    from NFFN import NextFreeFileName

稍后可能会添加pypi包。

贡献/包装

若要参与,请分叉存储库,提交更改并打开拉取请求。
这个包在PYPI有售。

要通过pypi发布新包,请发出以下命令:

  1. python3 setup.py sdist bdist_wheel
  2. python3 -m twine upload dist/*

兼容性

Python3.6.0及以上版本。

这个库利用f字符串PEP 498和 键入提示PEP 484

版权所有

麻省理工学院许可证

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Swing会在减少Windows时间后忽略第一次单击   java使用Jsoup从网页获取价格   JavaSpringbeans,使它们可用于其他类?我的组件为空?   java如何实现Servlet的渐进响应?   java使3个视图可见,然后根据条件隐藏其他视图   列出如何根据Java中ArrayList的大小为变量分配字符?   将p5js草图转换为处理的javascript问题   java如何在推送时对mongodb中的数组元素进行排序?   Java中基于字段将csv文件拆分为多个文件   java boolean onClickEventListener返回的目的   java开关和if/else的哪种组合更快?为什么?   java在其他静态方法中调用局部变量?   编码风格Java枚举应该在它们自己的文件中定义吗?   java绘制jtable单元格编辑器(位于jtable上方)   java是否可以在tomcat中编辑类文件并重新编译单个文件?   java使用基类中泛型方法的方法引用,这给了我NoSuchMethodError   Java泛型与使用参数化类有关   Java类型转换的加密输出   我对eclipse上的以下Java代码有一个问题(无法访问的代码)