interfacing the os in python
The most basic operation of any programming language is to interact with the operating system under which it is running. With a command-line-based operating system, such as unix or windows, interaction should include the extraction of other elements and text supplied on the command line. While we’re at it, it’s probably a good idea to actually parse the contents.
Beyond these basics, there are other more complex operations, including starting new processes, executing external programs, and handling multithreaded operations on those systems that support them.
python provides a unified entry point for most of this operation in the sys and os modules. There are also separate modules that that handle other systems such as time and getopt. We’ll have a look at all of these modules in this chapter and provide pointers to other chapters in this section that handle similar systems.
Working with the system (sys Module)
In a strict sence, the sys module provides tools for communicating directly with the system under which Python is being executed. Although it does provide some generic functionality (including access to the command-line arguments), its primary purpose is to help the programmer determine the environment under which their script is being executed.
Getting Command-line Arguments
In Python the command-line arguments are available through the sys argv array. As with other arrays, counting starts at zero, with the first element containing the name of the script and elements from index one onwards containing any arguments supplied to the script.
For example, the script
import sys count- for argument in sys.argv: print "Argument %d is %s" (count, argument) count + 1
generates the following output when executed:
$ python argv.py hello this is a test Argument 0 is argv.py Argument 1 is hello Argument 2 is this Argument 3 is is Argument 4 is a Argument 5 is test
Knowing the name of the script can be useful, especially if you want to report an error to the user.
Parsing Command-line Arguments
You may want to supply arguments to your script that can be parsed just like those supplied to other programs, such as:
process.py -y -g--debug --output=process.out myfile.txt
In this case you need to use the getopt module. The module provides a single function called getopt that processes the arguments, placing the information into some more convenient forms. The basic format of the function is
getopt(args, options, long options]}
where args is the list of arguments you want to parse, and options is a string containing the single-letter arguments you want to interpret. If you append a colon to a letter within the options string, then the argument will accept an additional argument as data to the argument. If supplied, the long _options should be a list of strings defining the words, rather than single letters, which should be identified and parsed. Note that in the arguments list, word-based arguments must be prefixed with a double, rather than a single, hyphen. The = suffix to an argument in long _options causes getopt to interpret the following argument as additional data.
The getopt function returns two objects. The first is a list of tuples containing the parsed argument and its value (if it has one); the second object is a list of the remaining, unparsed arguments.
>>> import getopt >>> args =['-a','-x','.bak'.'--debuglevel, 99, filel', 'file2"] >>> opts, remargs = getopt.getopt(args, "ax:', ['debuglevel=']) >>> opts [('-a', ''), ('-x', '.bak'), ('--debuglevel', '99')] >>> remargs ['filel', 'file2']
The getopt function raises an exception (GetoptError) if an unrecognized option is seen in the argument list, or if the argument is expecting additional data but doesn’t receive it.
Read More Topics:-