IT 117: Introduction to Scripting
Homework 6

Due

Sunday, March 5th at 11:59 PM

Deliverables

There is one deliverable for this assignment

Make sure the script obeys all the rules in the Script Requirements page.

Specification

The script must have 3 functions:

get_args

This function must have the following header:

def get_args(arg_number):

This function takes as its parameter an integer.

The function should look at the number of command line arguments that the script gets when it is run.

If the number of command line arguments is less than the parameter, it should print a usage message in the form specified in the Class Notes and the function should cause the script to quit.

If it gets the right number of command line arguments it should return those arguments.

Remember that the length of sys.argv is always one more than the number of command line arguments.

The test code will use the first command line argument as the argument to create_python_file and the second command line argument as the argument to print_directory.

create_python_file

This function must have the following header:

def create_python_file(filename):

This function takes as it's parameter a name of a Python script without the .py extension.

The first thing the function should do is check that there is no . (dot) in the filename.

If there is, the function should print an error message and the script should quit.

This error message must contain the word "ERROR".

Otherwise the function should add ".py" to the filename.

It should then use the Unix command touch to create a file with that filename.

touch when run with an argument creates a file of that name.

Then the function should give this file 755 permissions using the Unix chmod command.

print_directory

This function must have the following header:

def print_directory(path):

This function takes as it's parameter a path.

The first thing that the function should do is check to make sure the path is a directory that exits.

If the directory does not exist the function should print an error message and quit.

This error message must contain the word "ERROR".

Otherwise, the function should print the entries in the directory specified by the path parameter.

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:

filename, path = get_args(2)
print(filename, path)
create_python_file(filename)
print_directory(path)

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 hw5.py.
    Write a statement to import the os and sys modules.
    Enter the headers for get_args, create_python_file and print_directory.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Remove the pass statement from get_args.
    Replace it with an if statement that prints an error message if the length of sys.argv is less the value of the parameter arg_number plus 1.
    After the print statement, but still inside the if statement, write at statement that causes the script to quit. Copy the first line of the test code into your script at the bottom of the file.
    Run the script with no arguments.
    You should see the error message,
    Run the script with one argument.
    You should see the error message.
    Run the script with two arguments.
    You should not see your error message, but you will get a TypeError.
    Fix any errors you find.
  3. Outside the if statement write a return statement that that returns the first two elements in sys.argv.
    Copy the second line of the test code into your script at the bottom of the file.
    Run the script with two arguments.
    You should see those two arguments.
    Fix any errors you find.
  4. Replace the text in the print statement with the text of a usage message as described in Class Notes 10.
    Your message should have the same format as shown in the Class Notes and it should print only the name of the script, with no path. Run the script with no arguments.
    You should see
    Usage: hw6.py FILENAME PATH
    Fix any errors you find.
  5. Remove the pass statement from create_python_file.
    In its place write an if statement which prints an error message if the argument filename has a . (dot) in it.
    You need to do this to make sure that filename does not have an extension like ".py".
    This error message must have contain the string "ERROR" in it because my test script will check for it.
    After the print statement but still inside the if statement write a Python statement that will cause the script to quit.
    Outside the if statement print the filename parameter.
    Add the third line of the test code to the bottom of the script.
    Run the script with two arguments where the first argument has a ".py" extension.
    You should see something like this
    $ ./hw6.py dummy.py .
    dummy.py .
    ERROR: the filename cannot have an extension
    Run the script again with this time with a first argument without an extension.
    You should see something like this
    $ ./hw6.py dummy .
    dummy .
    dummy
  6. Remove the print statement added in the step above. Add the extension ".py" to the parameter filename.
    Create the string cmd_1 which has the Unix command touch followed by a space and filename.
    Print cmd_1.
    Create the string cmd_2 which contains the Unix command to give 755 permissions to filename.
    Print cmd_2.
    Run the script with two command line arguments.
    You should see something like this
    $ ./hw6.py dummy .
    dummy .
    touch dummy.py
    chmod 755 dummy.py
    Fix any errors you find.
  7. Remove the print statements added in the step above. Add two Python statements to run the Unix commands contained in cmd_1 and cmd_2.
    Run the script with two parameters.
    Check the results on the command line using ls -l. Fix any errors you find.
  8. Remove the pass statement from print_directory.
    Add an if statement that will print an error message if the parameter path is not a directory.
    This message must have contain the string "ERROR" in it because my test script will check for it.
    After the this print statement, add a statement that will cause the script to quit.
    Outside the if statement print path.
    Add the last test code statement to the bottom of the script.
    Run the script with an invalid pathname for the 2nd argument.
    You should see something like this
    $ ./hw6.py dummy xxxxxxxxxxxxxxx
    dummy xxxxxxxxxxxxxxx
    dummy.py
    Error:  xxxxxxxxxxxxxxx is not a directory
    Fix any errors you find.
  9. Remove the print statement added above.
    Create the variable contents and assign it the value of a list of the contents of the directory specified by path.
    Print contents.
    Run the script. Fix any errors you find.
  10. Remove the print statement added above.
    Add a for loop that print each entry in the directory.
    Run the script. Fix any errors you find.

Testing on Your Machine

Unix Setup

Copy the file to Unix

Testing the script on Unix

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