IT 117: Introduction to Scripting
Homework 6

Due

Sunday, March 8th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

get_command_line_arguments


print_path_dirs


python_file_count

Test Code

Output

Suggestions

  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.
    Print the value of sys.argv.
    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.
    You will need to use the first element in sys.argv and os.path.basename.
    See Usage Messages .
    Run the script with 0, 1 and 2 command line arguments.
    Fix any errors you find.
  5. Outside the if statement create the empty list values.
    You need to fill this list with the command line arguments.
    You can do this with a for loop that uses the range function to create indexes from 1 to the length of arg_number .
    Inside the loop use the index to get the value of the command line argument, then append it to values.
    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.
    Write an assignment statement that gives the variable path the value of the system variable PATH.
    To get this value you will need to use a variable in the os module that is a dictionary where system variable names are the keys and the values are the value of each system variable.
    Print path. 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 ;
    Create the list dirs by running the string split method on path.
    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 dirs.
    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 python_file_count.
    Add a statement that will change directory to the pathname contained the dir parameter.
    Create the list entries using a function in the os module that returns a list of everything inside a directory.
    Print this list.
    Uncomment the next three lines in the test code.
    Run the script.
    Fix any errors you find.
  10. Remove the print statement.
    Create the variable count and set it to 0.
    Write a for loop that iterates over entries.
    Inside the for loop write an if statement that print an entry if it is a file.
    There is a function in os.path that returns True if its argument is a file.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement. Replace it with an if statement that looks for files whose name ends in ".py".
    Use the string method endswith to do this. 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 3. Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.