Question: The script must have 3 functions: get_command_line_arguments print_path_dirs executable_file_count get_command_line_arguments This function must have the following header: def get_command_line_arguments(arg_number): This function is used to get

The script must have 3 functions:

  • get_command_line_arguments
  • print_path_dirs
  • executable_file_count

get_command_line_arguments

This function must have the following header:

def get_command_line_arguments(arg_number):

This function is used to get a certain number of command line arguments, specified by the parameter arg_number and return them as a tuple. If the user does not enter the proper number of the command line arguments, the code should print a usage message and exit the script. This message must have the following format

Usage: SCRIPT_NAME ARGUMENT_1 ARGUMENT_2

If the proper number of command line arguments has been provided, the function should return their values in a tuple.

print_path_dirs

This function must have the following header:

def print_path_dirs():

This function should print the pathnames of directories listed in the Unix PATH variable. Each pathname should be on a separate line.

executable_file_count

This function must have the following header:

def executable_file_count(dir):

This function should count the number of files with a .py extension and return that value.

Script for this assignment

Open an a text editor and create the file hw6.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw6.py file must contain the following test code at the bottom of the file:

get_command_line_arguments(2) agr1, arg2 = get_command_line_arguments(2) print(agr1, arg2) print() print_path_dirs() print() dir = "/home/ghoffman/course_files/it117_files" py_count = executable_file_count(dir) print("Python files", py_count)

Special Problems with Testing This Script

Because this script requires using the os and sys modules, you cannot test it the same way as in previous homework assignments.

The script needs command line arguments, which can be done using IDLE, but you must use the Run ... Customized item under the Run menu.

This will bring up a dialog box into which you can enter command line arguments for the script.

If you do this, you should be able to test get_command_line_arguments on your machine.

You can also test the second function, get_command_line_arguments, on your machine, but the results you get will be different from what I have shown you here.

That's because the value of the system variable PATH will be different on your machine then the value on pe15.

If you get something reasonable you should be OK, but you will need to test this again when you move the script to pe15.

The last function will executable_file_count will work on your machine, but the statement in the test code that calls this function will fail.

That's because it refers to a directory that is on pe15 but not on your machine.

To test this script you will have to move it to pe15.

Special Problem Testing print_path_dirs on Windows

If you are using a Windows machine, you will face another problem when testing print_path_dirs.

The PATH system variable contains a list of directories the shell must search to find the file needed to run a command.

Since the pathnames of many directories are contained in PATH, a special character is needed to separate one directory entry from the next.

On Unix and Macs this special character is :.

But on Windows it is ;.

When writing print_path_dirs you will have to use the string method split on the value of the PATH variable.

split needs to know the character that separates one directory entry from another.

A character used to separate one value from another in a string is called a delimiter.

If you run split without an argument it will use spaces as a delimiter.

When you run print_path_dirs on a Unix machine or Mac, the argument to split should be :.

But when testing this function on Windows, you should use ;.

If you test print_path_dirs on Windows, you will have to change the argument to split when you upload the file to the UMB Unix machine.

Suggestions

Write this program in a step-by-step fashion using the technique of incremental development.

In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.

  1. Create the file hw6.py. Import the os and sys modules. Enter the headers for each of the required functions. Under each header write the Python statement pass. Run the script. Fix any errors you find.
  2. Remove the pass statement from get_command_line_arguments. Write a statement that prints the list of things on the command line. Copy the test code to the bottom of the script. Comment out all but the first line of the code. Run the script with 0, 1 and 2 command line arguments. Fix any errors you find.
  3. Remove the print statement. Add an if statement that will execute if the user has entered fewer than the number of arguments specified by the parameter arg_number. Remember that the first entry in the list of things on the command line is the name of the script. So the length of the list of command line entries is NOT the number command line arguments. The body of this if statement should print "ERROR". Under the print statement write a statement that quits the script. Run the script with 0, 1 and 2 command line arguments. Fix any errors you find.
  4. Replace the error message with the usage message given above. Remember I want the name of the script not the pathname used to run it. Run the script with 0, 1 and 2 command line arguments. Fix any errors you find.
  5. Outside the if statement, return the first two command line arguments. Uncomment the next two lines in the test code. Run the script with 2 command line arguments. You should see the script print these two arguments. Fix any errors you find.
  6. Remove the pass statement from print_path_dirs. Add a print statement that prints the value of the system variable PATH. To get this value you will need to use a variable in the os module that points to a dictionary where system variable names are the keys and the value are the value of that system variable. Uncomment the next two lines in the test code. Run the script with 2 command line arguments. Fix any errors you find.
  7. Remove the print statement. The PATH system variable contains the absolute pathnames of directories holding executable files. Each of these pathnames is separated from the next with a :. On a Windows machine the character is ; See this note above. Create a list of directories using the string split method on the value of the PATH variable. You will have to give split the right argument to make this work. Print this list. Run the script with 2 command line arguments. Fix any errors you find.
  8. Remove the print statement. Replace it with a for loop that prints each directory in the list. Run the script with 2 command line arguments. You should see a list of directories on your machine. Fix any errors you find.
  9. Remove the pass statement from executable_file_count. Add statement that will change directory to the pathname contained the dir parameter. Add a statement that create a list of entries in the current directory. Print this list. Uncomment the next two lines in the test code. Move the script to your hw6 directory on pe15.cs.umb.edu. Run the script with 2 command line arguments. You should see a list of directories on pe15.cs.umb.edu . Fix any errors you find.
  10. Remove the print statement. Write a for loop that iterates through the list of directory entries. Inside the for loop write an if statement that print an entry if it is a file. Move the script to your hw6 directory on pe15.cs.umb.edu. Run the script with 2 command line arguments. You should see a list of files on pe15.cs.umb.edu. Fix any errors you find.
  11. Before the for loop, create the variable count and set is to 0. Remove the print statement. Replace it with an if statement that looks for files whose name ends in ".py". In the body of this if statement write a statement which increments count by 1. Outside the for loop return the value of count. Uncomment the last line of the test code. Run the script with 2 command line arguments. The number of ".py" files should be 7. Fix any errors you find.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!