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

hornet.file.read_csv(filename)
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.

Warning

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.
hornet.file.deserialize(filename)
Return the object or data structure in filename.

Miscellaneous

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().
hornet.file.list_dir(path)
Returns a list of all the files and directories in path.
hornet.file.remove(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

Warning

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.

hornet.file.join(*args)

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

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

Table Of Contents

Previous topic

hornet.event – Events

Next topic

hornet.loading – Module Loading

This Page