hornet.file – File Interactions

File API for easily IO interactions. Handles interactions (reading and writing) Comma Separated Value files, pickle files, etc. The interactions are designed so that a filename is passed in and all resource handling is handled within this module.

Module author: John Paulett <john.paulett -at- vanderbilt.edu>

Comma Separated Values

Returns a generator with the data of the CSV file, specified by filename as a list of lists. Ignores empty lines.
hornet.file.write_csv(filename, data, header=None)
Write a csv file to filename. data should be a 2D list of the data to write in the file. If a header is specified, the header row is written to the file first. The header should be a list of column names.

Object Persistence

Python provides a mechanism to serialize whole Python objects to disk. The object graphs can be of unlimited size and complexity.


Object persistence across Python or HORNET versions is not guaranteed to work.

hornet.file.serialize(filename, obj)
Persist the obj into a file specified by filename.
Return the object or data structure in filename.


hornet.file.tmp_file(*args, **kw)
Creates a temporary file and returns the absolute pathname of the temp file. The caller is responsible for removing once it is done with the file. Accepts all the arguments of mkstemp().
hornet.file.tmp_dir(*args, **kw)
Creates a temporary directory and returns the absolute pathname to the new directory. The caller is responsible for removing the directory. Accepts all the arguments of mkdtemp().
Returns a list of all the files and directories in path.

Deletes the file or directory specified by path. If it is a directory, all sub-directories and files will be removed. Designed to be equivalent to the unix command rm -rf


This method has not been extensively tested, especially on complex situations such as relative paths, subdirectories, symlinks, and non-Linux systems. Use at your own risk and make sure you have system backups.


Alias of join() that task a set of arguments and joins them using the approriate path separator.

>>> join('dir', 'file.csv')

Table Of Contents

Previous topic

hornet.event – Events

Next topic

hornet.loading – Module Loading

This Page