pipes
— Interface to shell pipelines外壳管道接口¶
Source code: Lib/pipes.py
Deprecated since version 3.11: The 管道模块已弃用(详见PEP 594)。pipes
module is deprecated (see PEP 594 for details).
The pipes
module defines a class to abstract the concept of a pipeline — a sequence of converters from one file to another.pipes
模块定义了一个类来抽象管道的概念——从一个文件到另一个文件的转换器序列。
Because the module uses /bin/sh command lines, a POSIX or compatible shell for 由于模块使用/bin/sh命令行,因此需要一个POSIX或os.system()
and os.popen()
is required.os.system()
和os.popen()
的兼容shell。
Availability: Unix. Not available on VxWorks.可用性:Unix。VxWorks上不可用。
The pipes
module defines the following class:pipes
模块定义以下类:
-
class
pipes.
Template
¶ An abstraction of a pipeline.管道的抽象。
Example:例子:
>>> import pipes
>>> t = pipes.Template()
>>> t.append('tr a-z A-Z', '--')
>>> f = t.open('pipefile', 'w')
>>> f.write('hello world')
>>> f.close()
>>> open('pipefile').read()
'HELLO WORLD'
Template Objects模板对象¶
Template objects following methods:使用以下方法创建模板对象:
-
Template.
reset
()¶ Restore a pipeline template to its initial state.将管道模板恢复到其初始状态。
-
Template.
clone
()¶ Return a new, equivalent, pipeline template.返回新的等效管道模板。
-
Template.
debug
(flag)¶ If flag is true, turn debugging on.如果flag为true
,则打开调试。Otherwise, turn debugging off. When debugging is on, commands to be executed are printed, and the shell is given否则,关闭调试。当调试打开时,将打印要执行的命令,并为shell提供set -x
command to be more verbose.set -x
命令以使其更加详细。
-
Template.
append
(cmd, kind)¶ Append a new action at the end.在末尾附加一个新动作。The cmd variable must be a valid bourne shell command.cmd变量必须是有效的bourne shell命令。The kind variable consists of two letters.kind变量由两个字母组成。The first letter can be either of第一个字母可以是'-'
(which means the command reads its standard input),'f'
(which means the commands reads a given file on the command line) or'.'
(which means the commands reads no input, and hence must be first.)'-'
(表示命令读取其标准输入)、'f'
(表示该命令读取命令行上的给定文件)或'.'
(这意味着命令没有读取输入,因此必须是第一个。)Similarly, the second letter can be either of类似地,第二个字母可以是'-'
(which means the command writes to standard output),'f'
(which means the command writes a file on the command line) or'.'
(which means the command does not write anything, and hence must be last.)'-'
(表示命令写入标准输出)、'f'
(表示该命令在命令行上写入文件)或'.'
(这意味着命令不写任何内容,因此必须是最后一个。)
-
Template.
prepend
(cmd, kind)¶ Add a new action at the beginning.在开头添加新操作。See有关参数的解释,请参阅append()
for explanations of the arguments.append()
。
-
Template.
open
(file, mode)¶ Return a file-like object, open to file, but read from or written to by the pipeline.返回一个类似于文件的对象,该对象打开到file,但由管道读取或写入。Note that only one of请注意,只能给出'r'
,'w'
may be given.'r'
、'w'
中的一个。
-
Template.
copy
(infile, outfile)¶ Copy infile to outfile through the pipe.通过管道复制infile到outfile。