Dana Vrajitoru
C151 Multi-User Operating Systems

Lab 11

Due Date: Monday, April 9, 2012.

Ex. 1. The sys module. Open an emacs window to edit a buffer called lab11.py.

a. Start the program by specifying the Python interpreter:

#!/usr/bin/python
Follow that line with a comment (each comment line must start with "#") containing your name and the lab number.

Add an instruction to import everything (*) from the module sys.

Add a main section to the file and leave it empty for now:

if __name__ == '__main__':

Before the main, write a function calls strs2int That converts an array of strings into an array of integers and returns it. This function should take one parameter (let's call it text) that we'll assume to be an array where each element is a string. Declare a result array that starts as empty. Use a for loop going over text as a sequence in which for every element you convert it to an integer by calling the built-in function int with this element as its argument, and then append the returned value to the result array. After the loop ends, return the result array.

In the main section, start by printing the content of the object argv with a comment saying what it is. Next, declare a variable size and assign it the value of the length of argv. Check if the length is greater than one, and if it is, then call the function strs2int with the argument argv[1:size] and store the result in a variable called numbers. The expression 1:size extracts a subarray within that range from the array argv. Since the indexes start form 0, this is ignoring the first element of the array. That is because when you examine the content of the argv that was printed out, you'll notice that the first element is the name of the script. Print a message with the content of this variable numbers (to make sure it's fine) and then print a message saying that the maximum of those number is, followed by the call to the built-in function max(numbers).

Save the script and execute it to make sure that it works. You'll have to supply a set of numbers as arguments in the execution of the script. For example, if your script is called lab11.py, then you can execute it with the command

lab11.py 1 2 3 4

b. Use the reference sys module reference (http://docs.python.org/lib/module-sys.html) to find out the meaning of the following variables defined in the module sys: executable, platform, version, path, maxint. Write a new function called sysInfo where you print out the content of these variable with a small text explaining them. Add a couple more variables that seem interesting to you.

For example, for the variable version, we find out from the documentation that it contains information about the version of the Python interpreter installed on the computer. So we add a line to that function containing

print "This is the Python interpreter version", version

Add a call to this function in the main section. Save the script and execute it.

Ex. 2. The os module. In the same script, import everything from the module os.

a. Add a few more lines to the function sysInfo containing a print out of the content of environ['USER'] (your user name), and the same for the environment variable $HOME.

In the same function, call the function uname() and store it in a local variable, say u. Then print out the elements 0 and 1 of that array u with a comment saying that they represent the names of the operating system and of the computer. See this documentation (http://docs.python.org/lib/os-procinfo.html) for more information.

b. Executing some Linux commands. The function system(cmd) defined in the module os calls a Linux command (in our case) where the parameter cmd contains a string with the command to be executed. Use it to list the files in the current directory in the main section. The command that is the argument of this function should be specified as a simple string.

The documentation on file operations in os (http://docs.python.org/lib/os-file-dir.html) describes some functions that can manipulate files and directories. Use it to get a list of files in the current directory and to print it out (the path can be specified as "."). What is the difference between calling this function and executing the system command as before?

Save the script and execute it to make sure that it works.

Upload: To Oncourse the file lab11.py and answer the question in the paragraph above the last sentence in the text of the submission.