IT 117: Introduction to Scripting
Homework 6
Due
Sunday, March 8th 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
- Copy the directory hw6_test_dir
from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
- This script will contain functions that use the
os and sys
modules
- Though the script is designed to run on
pe15.cs.umb.edu it can be run on your
machine with a few adjustments
- The script must contain 3 functions
- get_command_line_arguments
- print_path_dirs
- python_file_count
Functions
get_command_line_arguments
print_path_dirs
- Header
def print_path_dirs():
- This function must print the directories listed in the
PATH system variable
- Each directory must be on a separate line
python_file_count
Test Code
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.
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
- If you have changed the argument to
spit()
from : to ;,
change it back to :
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it117 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw6 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
- Click and drag the directory
hw6_test_dir from the
bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.