- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
Python Interface to Shell Pipelines
To use the UNIX command pipeline mechanism using python. In the command pipelining a sequence converts from one file to another file.
This module uses /bin/sh command line. So we need os.system() and os.popen() methods.
To use this module, we should import it using −
The pipes holds Template class −
This class is basically an abstraction of a pipeline. It has different methods. These are as follows.
This method is used to restore the pipeline template to its initial position.
This method is used to create another new, and same template object.
This method is used to Debug the process. When the flag is true, the debugging mode is on. When it is on, the commands will be printed during execution.
Method Template.append(command, kind)
This method is used to insert a new task at the end. The command must be a bourne shell command. The kind variable consists of two characters.
For the first letter, it means −
|Sr.No.||Character & Description|
Command reads the standard Input
Command will read a given file on the command line
Command does not read any input. So it will be at the first position.
For the second letter, it means.
|Sr.No.||Character & Description|
Command writes to the standard output
Command will write a file on the command line
Command does not write any output. So it will be at the last position.
Method Template.prepend(command, kind)
This method is used to insert a new task at the beginning. The command must be a bourne shell command. It is similar to the append() method.
Method Template.open(file, mode)
This method is used to open a file to read or write. But reading or writing operations are done by the pipelines.
Method Template.copy(infile, outfile)
This method is used to copy from infile to outfile, by the pipeline.
import pipes my_template = pipes.Template() my_template.append('tr a-z A-Z', '--') my_template.prepend('echo Python Programming', '--') #Prepend the item into queue my_template.append('rev', '--') my_template.debug(True) my_file = my_template.open('test_file', 'w') my_file.close() content = open('test_file').read() print(content)
$ python3 example.py echo Python Programming | tr a-z A-Z | rev >test_file + rev + tr a-z A-Z + echo Python Programming GNIMMARGORP NOHTYP
- Related Articles
- How to clear Python shell?
- Python Program to Implement Shell Sort
- How to execute a Python file in Python shell?
- Python GNU readline Interface
- Python Interface to UNIX syslog library routines
- How to know/change current directory in Python shell?
- Python interface for SQLite databases
- Python Garbage Collector interface (gc)
- What is logical layout of FX Pipelines in Computer Architecture?
- What is the implementation of FX Pipelines in Computer Architecture?
- Command Line Interface Programming in Python?
- Low-level networking interface in Python (socket)
- Shell Sort
- Shell Model
- Sound-playing interface for Windows in Python (winsound)