Python Concepts/Interfacing with Unix

Objective

 * To describe how to invoke and pass arguments to a python script on the Unix command line.
 * To describe how to invoke a Unix command from within a Python script.

Invoking python from Unix
To enter python in interactive mode:

If your $PATH global variable contains the directory /usr/local/bin, you can invoke python with a file name rather than a path name:

Let test.py be the name of a python script.

Let the first line of your script contain the path name of python with appropriate syntax:

Invoking Unix from python
This section uses function  to pass a command to the Unix command line. Then we review the result of executing (or trying to execute) the Unix command. This section introduces several new objects such as CompletedProcess and the various error conditions and subclasses of those conditions.

The Unix command  is simple, can be invoked without arguments and is not likely to fail:

The  command above displayed the date and returned code 0. It was invoked and went to normal completion. To capture the output of the invocation:

To execute a Unix command with arguments:

Notice the difference between the next two commands:

Sending data to a pipe
This process depends on the Unix command  On my system   produced inconsistent results:

Python executable  produced consistent, predictable results across all 4 shells.

The perl script below will be formatted and sent to a pipe for execution by perl.

Processing errors
If you execute command  and there is an error, the value of   remains unchanged. This could be confusing later. The following code works well:

CalledProcessError
If check is true, and the process exits with a non-zero exit code, a CalledProcessError exception will be raised.

Normal processing of errors
If you're not using the  or   arguments, most errors, perhaps all, can be processed by referring to the contents of the  object.

In the above invocation of subprocess.run(...), the exception was not taken and all information about the error is available in a.returncode and a.stderr.

With a few well chosen Unix commands, subprocess.run gives you easy access to much good information about the Unix environment.

try and except statements provide good clean info about errors:

Without try and except:

Default value shell=False
The reference recommends default value  wherever possible because the method manages the conversion of python string to Unix string so that the Unix executable receives an accurate copy of each argument supplied.

However:

Try again with

'$t' and '$abcde' are missing from output above because a Unix string beginning with '"' provides variable substitution.

Unix strings and Python strings
Provided that the string to be passed to Unix does not contain a single quote "'", the simplest way to prepare the string is to enclose it in single quotes:

If the string contains a single quote, convert the single quote to a pattern which Unix will recognize.

Assignments


Modify the python script so that data sent to the pipe does not contain blank lines or comments.
 * Under "Sending data to a pipe" above:

Build the Unix command in different ways such as the following and check the results. Try the following Unix commands and verify that a.stdout.decode matches perlScript exactly:

Further Reading or Review

 * Previous Lesson: Using Python's Decimal Module
 * This lesson: Interfacing with Unix
 * Next Lesson: Special Methods
 * Course Home Page