以线程安全的方式查找文件序列中的下一个文件。
seqfile的Python项目详细描述
以线程安全的方式生成顺序文件名。
用法
如果要创建要将输出写入(例如从 不同的线程或同一程序的连续运行),然后 您可以在程序中或从命令中使用此模块 行。
从命令行
$ seqfile usage: seqfile [-h] [-m MAX_ATTEMPTS] [-b BASE] prefix [suffix] [folder] seqfile: error: too few arguments # Assume files "./a.0.txt" and "./a.3.txt" exist. $ seqfile a. .txt . ./a.4.txt # Assume no other files exist. # This will create files 100 files with names test.*.txt in the current # folder. $ seq 100 | xargs -n 1 -P 100 -I{} \ bash -c "fname=\$(seqfile test. .txt); \ echo 'Do something awesome with {}' > \$fname"
从您的程序
# Assume files "./a.0.txt" and "./a.3.txt" exist. >> import seqfile # Providing prefix and suffix finds the next file in the sequence # ignoring any gaps >> seqfile.findNextFile('.', prefix='a.', suffix='.txt') './a.4.txt' # Providing a file name generator as fnameGen produces the first file # which it was successful in creating. >> seqfile.findNextFile('.', fnameGen=lambda x: 'a.' + str(x) + '.txt') './a.1.txt'
返回的文件将存在,为空,可以打开以写入。
安装
此软件包可从pypi<;https://pypi.python.org/pypi/seqfile>;获得。
pip install seqfile
您可以直接从源安装放气边缘:
pip install git+https://github.com/musically-ut/seqfile.git@master#egg=seqfile
测试
可以使用nosetests运行测试(如果所有依赖项都安装在 或者使用python setup.py test。
注意事项
最好不要在prefix、suffix或 folder。尽管该支持已经在所有主要的操作系统上测试过,但它还没有 已确认所有操作系统/文件系统组合都将工作。
此外,原子地创建文件所使用的o_u excel技巧可能不会 在向nfs写入时处理旧的linux内核。