Dana Vrajitoru
C151 Multi-User Operating Systems

C151 Lab 13

Due Date: Monday, April 26, 2021.

Ex. 1. The sys Module

Open an emacs window to edit a buffer called lab13.py, or edit it using the editor of your choice.

a. Using Positional Parameters

Start the program by specifying the Python interpreter:

#!/usr/bin/python

This should look familiar by now.

Follow that line with a comment containing your name and the lab number.

Note: each comment line must start with "#".

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

from sys import *

We have used a similar syntax to import the module string before.

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

if __name__ == '__main__':

We will add instructions to it later.

Before the main, write a function called 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:

result = []

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.

We can add elements to this array later by calling result.append(...).

In the main section, start by printing the content of the object argv (imported from the sys module) with a comment saying what it is.

Note: the object argv is imported from the sys module.

Next, declare a variable called size and assign it the value of the length of argv.

If you recall, the size of an array is given by the function len.

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 means that we are leaving out 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 numbers is, followed by the call to the built-in function max(numbers).

This function max is a built-in implementation of the function maxArray that you had to write for Homework 11.

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 lab13.py, then you can execute it with the command

lab13.py 1 2 3 4

b. System Information

Use the reference sys module reference (https://docs.python.org/3/library/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 variables with a small text explaining what each of them is. 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 can 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. Environment Variables

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

We can access the same way any of the environment variables defined in bash.

In the same function, call the function uname() and store the result 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 (https://docs.python.org/2.7/library/os.html#module-os) for more information.

b. Executing 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 the function system(...) to list the files in the current directory in the main section. The command that is passed in as the argument of this function should be provided as a simple string (in quotes).

The documentation on file operations in os (https://docs.python.org/2.7/library/os.html#files-and-directories) describes some functions that can manipulate files and directories.

Find in the documentation above a function that allows you to get a list of files in the current directory. Call it and print out the result.

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.

Lab Submission

Upload to Canvas: the file lab13.py.