IT 117: Introduction to Scripting
Homework 6

Due

Sunday, October 12th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

get_2_args


create_python_file


print_directory

Test Code

arg1, arg2 = get_2_args()
print(arg1, arg2)
create_python_file(arg1)
print_directory(arg2)

Output

Suggestions

  1. Create the file hw6.py.
    Write statements to import the os and sys modules.
    Enter the headers for get_2_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_2_args.
    Replace it with an if statement that tests whether the length of sys.argv is less than 3.
    Inside the if statement, print an error message.
    After the print statement, but still inside the if statement, call a sys function that will cause the script to quit.
    Copy the test code into your script at the bottom of the file.
    Comment out all but the first line of the test code.
    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.
    Uncomment 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 error text in the print statement with the arguments that will print a usage message.
    To learn how to do this, see 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, without a 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.
    Uncomment 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 .
  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.