IT 117: Introduction to Scripting
Homework 6
Due
Sunday, October 12th at 11:59 PM
What You Need to Do
- Create the script hw6.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw6
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw6.py
Specification
- This script will contain functions that use
code contained in the os and
sys modules
- The script must contain 3 functions
- get_2_args
- create_python_file
- print_directory
Functions
get_2_args
- Header
def get_2_args():
- The function should check that it gets at least 2
command line arguments.
- If the number of command line arguments is less than 2,
it should print a usage message
- And then it should cause the script to quit
- To see what that message should look like go
here
- If there are at least two command line arguments
the script 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
print_directory
Test Code
- Your script must contain the following statements
- They should appear at the bottom of your script
arg1, arg2 = get_2_args()
print(arg1, arg2)
create_python_file(arg1)
print_directory(arg2)
Output
Suggestions
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
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.
-
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.
-
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.
-
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.
-
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 .
-
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.
-
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.
-
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.
-
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.
-
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
- This script cannot be run inside IDLE
- You will have to run this script at the command line
- If you have a Mac run Terminal
- Go to the directory that holds your script
- The script will have to be run more than once to test the error checking
- You should see when run with no command line arguments
$ ./hw6.py
Usage: hw06.py requires 2 arguments
- When run with two arguments, but the first argument has an extension,
you should see
$ ./hw6.py dummp.py foo
dummp.py foo
ERROR: The filename should not have an extension
- When run with 2 arguments, but the second argument is path that does
not exists, you should see
$ ./hw6.py dummy xxxxxxxx
dummy xxxxxxxx
ERROR: xxxxxxxx is not a directory
- When run with two valid arguments, you should see the the created
script file in the output
$ ./hw6.py dummy . # the second argument is "dot"
dummy .
dummy.py
hw6.py
Unix Setup
- Log in to users3.cs.umb.edu
You will be in your home directory.
- Go to your it117 directory
cd it117
- Go to your hw directory
cd hw
- Create a directory for this exercise
mkdir hw6
- Check that the directory was created
ls
Copy the file to Unix
- Open FileZilla and connect to
users3.cs.umb.edu
You will have to connect using your Unix username and password.
- Copy the file to the to it117/hw/hw6
Testing the script on Unix
- Connect to
Use an ssh client.
- Go to the directory for this exercise
cd it117/hw/hw6
- Make this script executable
chmod 755 hw6.py
- You should see when run with no command line arguments
$ ./hw6.py
Usage: hw06.py requires 2 arguments
- When run with two arguments, but the first argument has an extension, you should see
$ ./hw6.py dummy.py dummp.py foo
dummp.py foo
ERROR: The filename should not have an extension
- When run with 2 arguments, but the second argument is path that does not exists, you should see
$ ./hw6.py dummy xxxxxxxx
dummy xxxxxxxx
ERROR: xxxxxxxx is not a directory
- When run with two valid arguments, you should see the the created script file in the output
$ ./hw6.py dummy . # the second argument is "dot"
dummy .
dummy.py
hw6.py
- If your script does not run on users3
you will lose points
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.