1
:mod:`pipes` --- Interface to shell pipelines
2
=============================================
6
:synopsis: A Python interface to Unix shell pipelines.
7
.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
9
**Source code:** :source:`Lib/pipes.py`
13
The :mod:`pipes` module defines a class to abstract the concept of a *pipeline*
14
--- a sequence of converters from one file to another.
16
Because the module uses :program:`/bin/sh` command lines, a POSIX or compatible
17
shell for :func:`os.system` and :func:`os.popen` is required.
19
The :mod:`pipes` module defines the following class:
24
An abstraction of a pipeline.
29
>>> t = pipes.Template()
30
>>> t.append('tr a-z A-Z', '--')
31
>>> f = t.open('pipefile', 'w')
32
>>> f.write('hello world')
34
>>> open('pipefile').read()
43
Template objects following methods:
46
.. method:: Template.reset()
48
Restore a pipeline template to its initial state.
51
.. method:: Template.clone()
53
Return a new, equivalent, pipeline template.
56
.. method:: Template.debug(flag)
58
If *flag* is true, turn debugging on. Otherwise, turn debugging off. When
59
debugging is on, commands to be executed are printed, and the shell is given
60
``set -x`` command to be more verbose.
63
.. method:: Template.append(cmd, kind)
65
Append a new action at the end. The *cmd* variable must be a valid bourne shell
66
command. The *kind* variable consists of two letters.
68
The first letter can be either of ``'-'`` (which means the command reads its
69
standard input), ``'f'`` (which means the commands reads a given file on the
70
command line) or ``'.'`` (which means the commands reads no input, and hence
73
Similarly, the second letter can be either of ``'-'`` (which means the command
74
writes to standard output), ``'f'`` (which means the command writes a file on
75
the command line) or ``'.'`` (which means the command does not write anything,
76
and hence must be last.)
79
.. method:: Template.prepend(cmd, kind)
81
Add a new action at the beginning. See :meth:`append` for explanations of the
85
.. method:: Template.open(file, mode)
87
Return a file-like object, open to *file*, but read from or written to by the
88
pipeline. Note that only one of ``'r'``, ``'w'`` may be given.
91
.. method:: Template.copy(infile, outfile)
93
Copy *infile* to *outfile* through the pipe.