Package lib :: Module io
[hide private]
[frames] | no frames]

Module io

source code

Classes [hide private]
  DummyFileObject
  SplitIO
Functions [hide private]
 
delete(file_name, dir=None, fail=True)
Deleting the given file, taking into account missing compression extensions.
source code
(int, str)
determine_compression(file_path)
Function for determining the compression type, and for also testing if the file exists.
source code
list of lists of str
extract_data(file=None, dir=None, file_data=None, sep=None)
Return all data in the file as a list of lines where each line is a list of line elements.
source code
str
file_root(file_path)
Return the root file name, striped of path and extension details.
source code
str
get_file_path(file_name=None, dir=None)
Generate and expand the full file path.
source code
 
io_streams_restore(verbosity=1)
Restore all IO streams to the Python defaults.
source code
 
io_streams_log(file_name=None, dir=None, verbosity=1)
Turn on logging, sending both STDOUT and STDERR streams to a file.
source code
 
io_streams_tee(file_name=None, dir=None, compress_type=0, verbosity=1)
Turn on teeing to split both STDOUT and STDERR streams and sending second part to a file.
source code
 
mkdir_nofail(dir=None, verbosity=1)
Create the given directory, or exit without raising an error if the directory exists.
source code
file object
open_read_file(file_name=None, dir=None, verbosity=1)
Open the file 'file' and return all the data.
source code
writable file object (if return_path, then a tuple of the writable file and the full file path)
open_write_file(file_name=None, dir=None, force=False, compress_type=0, verbosity=1, return_path=False)
Function for opening a file for writing and creating directories if necessary.
source code
list of str
sort_filenames(filenames=None, rev=False)
Sort the given list in alphanumeric order.
source code
list of lists of str
strip(data, comments=True)
Remove all comment and empty lines from the file data structure.
source code
str
swap_extension(file=None, ext=None)
Swap one file name extension for another.
source code
 
test_binary(binary)
Function for testing that the binary string corresponds to a valid executable file.
source code
 
write_data(out=None, headings=None, data=None, sep=None)
Write out a table of the data to the given file handle.
source code
Variables [hide private]
  message = sys.exc_info() [1]
  bz2_module_message = message.args [0]
  __package__ = None
hash(x)

Imports: sys, bz2, devnull, F_OK, X_OK, access, altsep, getenv, makedirs, pathsep, remove, sep, expanduser, basename, splitext, isfile, search, split, stdin, stdout, stderr, warn, is_filetype, bz2_open, gz_open, RelaxError, RelaxFileError, RelaxFileOverwriteError, RelaxMissingBinaryError, RelaxNoInPathError, RelaxNonExecError, RelaxWarning


Function Details [hide private]

delete(file_name, dir=None, fail=True)

source code 

Deleting the given file, taking into account missing compression extensions.

Parameters:
  • file_name (str) - The name of the file to delete.
  • dir (None or str) - The directory containing the file.
  • fail (bool) - A flag which if True will cause RelaxFileError to be raised.
Raises:
  • RelaxFileError - If the file does not exist, and fail is set to true.

determine_compression(file_path)

source code 

Function for determining the compression type, and for also testing if the file exists.

Parameters:
  • file_path (str) - The full file path of the file.
Returns: (int, str)
A tuple of the compression type and full path of the file (including its extension). A value of 0 corresponds to no compression. Bzip2 compression corresponds to a value of 1. Gzip compression corresponds to a value of 2.

extract_data(file=None, dir=None, file_data=None, sep=None)

source code 

Return all data in the file as a list of lines where each line is a list of line elements.

Parameters:
  • file (str or file object) - The file to extract the data from.
  • dir (str or None) - The path where the file is located. If None and the file argument is a string, then the current directory is assumed.
  • file_data (list of str) - If the file data has already been extracted from the file, it can be passed into this function using this argument. If data is supplied here, then the file_name and dir args are ignored.
  • sep (str) - The character separating the columns in the file data. If None, then whitespace is assumed.
Returns: list of lists of str
The file data.

file_root(file_path)

source code 

Return the root file name, striped of path and extension details.

Parameters:
  • file_path (str) - The full path to the file.
Returns: str
The file root (with all directories and the extension stripped away).

get_file_path(file_name=None, dir=None)

source code 

Generate and expand the full file path.

Parameters:
  • file_name (str) - The name of the file to extract the data from.
  • dir (str) - The path where the file is located. If None, then the current directory is assumed.
Returns: str
The full file path.

io_streams_restore(verbosity=1)

source code 

Restore all IO streams to the Python defaults.

Parameters:
  • verbosity (int) - The verbosity level.

io_streams_log(file_name=None, dir=None, verbosity=1)

source code 

Turn on logging, sending both STDOUT and STDERR streams to a file.

Parameters:
  • file_name (str) - The name of the file.
  • dir (str) - The path where the file is located. If None, then the current directory is assumed.
  • verbosity (int) - The verbosity level.

io_streams_tee(file_name=None, dir=None, compress_type=0, verbosity=1)

source code 

Turn on teeing to split both STDOUT and STDERR streams and sending second part to a file.

Parameters:
  • file_name (str) - The name of the file.
  • dir (str) - The path where the file is located. If None, then the current directory is assumed.
  • compress_type (int) - The compression type. The integer values correspond to the compression type: 0, no compression; 1, Bzip2 compression; 2, Gzip compression.
  • verbosity (int) - The verbosity level.

mkdir_nofail(dir=None, verbosity=1)

source code 

Create the given directory, or exit without raising an error if the directory exists.

Parameters:
  • dir (str) - The directory to create.
  • verbosity (int) - The verbosity level.

open_read_file(file_name=None, dir=None, verbosity=1)

source code 

Open the file 'file' and return all the data.

Parameters:
  • file_name (str) - The name of the file to extract the data from.
  • dir (str) - The path where the file is located. If None, then the current directory is assumed.
  • verbosity (int) - The verbosity level.
Returns: file object
The open file object.

open_write_file(file_name=None, dir=None, force=False, compress_type=0, verbosity=1, return_path=False)

source code 

Function for opening a file for writing and creating directories if necessary.

Parameters:
  • file_name (str) - The name of the file to extract the data from.
  • dir (str) - The path where the file is located. If None, then the current directory is assumed.
  • force (bool) - Boolean argument which if True causes the file to be overwritten if it already exists.
  • compress_type (int) - The compression type. The integer values correspond to the compression type: 0, no compression; 1, Bzip2 compression; 2, Gzip compression. If no compression is given but the file name ends in '.gz' or '.bz2', then the compression will be automatically set.
  • verbosity (int) - The verbosity level.
  • return_path (bool) - If True, the function will return a tuple of the file object and the full file path.
Returns: writable file object (if return_path, then a tuple of the writable file and the full file path)
The open, writable file object and, if the return_path is True, then the full file path is returned as well.

sort_filenames(filenames=None, rev=False)

source code 

Sort the given list in alphanumeric order. Should be equivalent to unix 'ls -n' command.

Parameters:
  • filenames (list of str) - The list of file names to sort.
  • rev (bool) - Flag which if True will cause the list to be reversed.
Returns: list of str
The sorted list of file names.

strip(data, comments=True)

source code 

Remove all comment and empty lines from the file data structure.

Parameters:
  • data (list of lists of str) - The file data to clean up.
  • comments (bool) - A flag which if True will cause comments to be deleted.
Returns: list of lists of str
The input data with the empty and comment lines removed.

swap_extension(file=None, ext=None)

source code 

Swap one file name extension for another.

Parameters:
  • file (str) - The name of the original file.
  • ext (str) - The new file name extension to use.
Returns: str
The name of the file with the new file name extension.

test_binary(binary)

source code 

Function for testing that the binary string corresponds to a valid executable file.

Parameters:
  • binary (str) - The name or path of the binary executable file.

write_data(out=None, headings=None, data=None, sep=None)

source code 

Write out a table of the data to the given file handle.

Parameters:
  • out (file handle) - The file handle to write to.
  • headings (list of str or None) - The optional headings to print out.
  • data (list of list of str) - The data to print out.
  • sep (str or None) - The column separator which, if None, defaults to whitespace.