There is one deliverable for this assignment
The script must have 3 functions:
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.
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.
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.
Open an a text editor and create the file hw6.py.
You can use the editor built into IDLE or a program like Sublime.
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)
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.
pass
. pass
statement from get_args. if
statement that prints an error message if the length of
sys.argv
is less the value of the parameter arg_number plus 1. 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. if
statement write a return
statement that that returns
the first two elements in sys.argv
. print
statement with the text of a usage message as
described in Class Notes 10. Usage: hw6.py FILENAME PATHFix any errors you find.
pass
statement from create_python_file. if
statement which prints an error message if the argument
filename has a . (dot) in it. print
statement but still inside the if
statement write a
Python statement that will cause the script to quit. if
statement print the filename parameter. $ ./hw6.py dummy.py . dummy.py . ERROR: the filename cannot have an extensionRun the script again with this time with a first argument without an extension.
$ ./hw6.py dummy . dummy . dummy
print
statement added in the step above.
Add the extension ".py" to the parameter filename. touch
followed by a space and filename. $ ./hw6.py dummy . dummy . touch dummy.py chmod 755 dummy.pyFix any errors you find.
print
statements added in the step above.
Add two Python statements to run the Unix commands contained in
cmd_1 and cmd_2. ls -l
.
Fix any errors you find.
pass
statement from print_directory. if
statement that will print an error message if the parameter
path is not a directory. print
statement, add a statement that will cause
the script to quit. if
statement print path. $ ./hw6.py dummy xxxxxxxxxxxxxxx dummy xxxxxxxxxxxxxxx dummy.py Error: xxxxxxxxxxxxxxx is not a directoryFix any errors you find.
print
statement added above. for
loop that print each entry in the directory. $ ./hw6.py Usage: hw06.py requires 2 arguments
$ ./hw6.py dummp.py foo dummp.py foo ERROR: The filename should not have an extension
$ ./hw6.py dummy xxxxxxxx dummy xxxxxxxx ERROR: xxxxxxxx is not a directory
$ ./hw6.py dummy . # the second argument is "dot" dummy . dummy.py hw6.py
cd it117
cd hw
mkdir hw6
ls
cd it117/hw/hw6
chmod 755 hw6.py
$ ./hw6.py Usage: hw06.py requires 2 arguments
$ ./hw6.py dummy.py dummp.py foo dummp.py foo ERROR: The filename should not have an extension
$ ./hw6.py dummy xxxxxxxx dummy xxxxxxxx ERROR: xxxxxxxx is not a directory
$ ./hw6.py dummy . # the second argument is "dot" dummy . dummy.py hw6.py
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.