IPython Documentation

Table Of Contents

Previous topic

utils.importstring

Next topic

utils.ipstruct

This Page

utils.io

Module: utils.io

Inheritance diagram for IPython.utils.io:

IO related utilities.

Classes

CapturedIO

class IPython.utils.io.CapturedIO(stdout, stderr)

Bases: object

Simple object for containing captured stdout/err StringIO objects

__init__(stdout, stderr)
show()

write my output to sys.stdout/err as appropriate

stderr
stdout

IOStream

class IPython.utils.io.IOStream(stream, fallback=None)
__init__(stream, fallback=None)
close()
closed
write(data)
writelines(lines)

IOTerm

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)

NLprinter

class IPython.utils.io.NLprinter

Print an arbitrarily nested list, indicating index numbers.

An instance of this class called nlprint is available and callable as a function.

nlprint(list,indent=’ ‘,sep=’: ‘) -> prints indenting each level by ‘indent’ and using ‘sep’ to separate the index from the value.

__init__()

Tee

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.

capture_output

class IPython.utils.io.capture_output(stdout=True, stderr=True)

Bases: object

context manager for capturing stdout/err

__init__(stdout=True, stderr=True)
stderr = True
stdout = True

Functions

IPython.utils.io.ask_yes_no(prompt, default=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. 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.file_read(filename)

Read a file and close it. Returns the file source.

IPython.utils.io.file_readlines(filename)

Read a file and close it. Returns the file source using readlines().

IPython.utils.io.raw_input_ext(prompt='', ps2='... ')

Similar to raw_input(), but accepts extended lines if input ends with .

IPython.utils.io.raw_input_multi(header='', ps1='==> ', ps2='..> ', terminate_str='.')

Take multiple lines of input.

A list with each line of input as a separate element is returned when a termination string is entered (defaults to a single ‘.’). Input can also terminate via EOF (^D in Unix, ^Z-RET in Windows).

Lines of input which end in are joined into single entries (and a secondary continuation prompt is issued as long as the user terminates lines with ). This allows entering very long strings which are still meant to be treated as single entities.

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