IPython Documentation

Table Of Contents

Previous topic

Module: utils.importstring

Next topic

Module: utils.ipstruct

This Page

Note

This documentation is for a development version of IPython. There may be significant differences from the latest stable release.

Module: utils.io

IO related utilities.

3 Classes

class IPython.utils.io.IOStream(stream, fallback=None)
__init__(stream, fallback=None)
class IPython.utils.io.IOTerm(stdin=None, stdout=None, stderr=None)

Term holds the file or file-like objects for handling I/O operations.

These are normally just sys.stdin, sys.stdout and sys.stderr but for Windows they can can replaced to allow editing the strings before they are displayed.

__init__(stdin=None, stdout=None, stderr=None)
class IPython.utils.io.Tee(file_or_name, mode='w', channel='stdout')

Bases: object

A class to duplicate an output stream to stdout/err.

This works in a manner very similar to the Unix ‘tee’ command.

When the object is closed or deleted, it closes the original file given to it for duplication.

__init__(file_or_name, mode='w', channel='stdout')

Construct a new Tee object.

Parameters:

file_or_name : filename or open filehandle (writable)

File that will be duplicated

mode : optional, valid mode for open().

If a filename was give, open with this mode.

channel : str, one of [‘stdout’, ‘stderr’]

close()

Close the file and restore the channel.

flush()

Flush both channels.

write(data)

Write data to both channels.

6 Functions

IPython.utils.io.ask_yes_no(prompt, default=None, interrupt=None)

Asks a question and returns a boolean (y/n) answer.

If default is given (one of ‘y’,’n’), it is used if the user input is empty. If interrupt is given (one of ‘y’,’n’), it is used if the user presses Ctrl-C. Otherwise the question is repeated until an answer is given.

An EOF is treated as the default answer. If there is no default, an exception is raised to prevent infinite loops.

Valid answers are: y/yes/n/no (match is not case sensitive).

IPython.utils.io.temp_pyfile(src, ext='.py')

Make a temporary python file, return filename and filehandle.

Parameters:

src : string or list of strings (no need for ending newlines if list)

Source code to be written to the file.

ext : optional, string

Extension for the generated file.

Returns:

(filename, open filehandle)

It is the caller’s responsibility to close the open file and unlink it.

IPython.utils.io.atomic_writing(*args, **kwds)

Context manager to write to a file only if the entire write is successful.

This works by creating a temporary file in the same directory, and renaming it over the old file if the context is exited without an error. If the target file is a symlink or a hardlink, this will not be preserved: it will be replaced by a new regular file.

On Windows, there is a small chink in the atomicity: the target file is deleted before renaming the temporary file over it. This appears to be unavoidable.

Parameters:

path : str

The target file to write to.

text : bool, optional

Whether to open the file in text mode (i.e. to write unicode). Default is True.

encoding : str, optional

The encoding to use for files opened in text mode. Default is UTF-8.

**kwargs

Passed to io.open().

IPython.utils.io.raw_print(*args, **kw)

Raw print to sys.__stdout__, otherwise identical interface to print().

IPython.utils.io.raw_print_err(*args, **kw)

Raw print to sys.__stderr__, otherwise identical interface to print().

IPython.utils.io.unicode_std_stream(stream='stdout')

Get a wrapper to write unicode to stdout/stderr as UTF-8.

This ignores environment variables and default encodings, to reliably write unicode to stdout or stderr.

unicode_std_stream().write(u'ł@e¶ŧ←')