Skip to content
⋮ Generate sequential file names in a thread-safe way.
Python PowerShell Batchfile
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.



BuildStatus BuildStatusWin Coverage

Generate sequential file names in a thread-safe way.


If you want to create sequential files to write output to (say from different threads or from consecutive runs of the same program), then you can use this module from within your program or from the command line.

From the commandline

$ 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 .

# 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"

From your program

# 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')

# 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')

The returned file will exist, will be empty, and can be opened for writing.


This package is available from PyPi.

pip install seqfile

You can install the bleeding edge directly from the source:

pip install git+


The tests can be run using nosetests (if all dependencies are installed in the environment) or using python test.


It is best not to provide unicode characters in the prefix, suffix, or folder. Though the support has been tested on all major OSes, it has not been verfied that all OS/filesystem combinations will work.

Also, the O_CREAT | O_EXCEL trick used to create files atomically may not work on old linux kernels while writing to an NFS.

You can’t perform that action at this time.