IT 117: Introduction to Scripting
Homework 6
Due
Sunday, October 13th 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
- 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 must have 4 functions
- script_filename
- system_var
- count_files
- executable_files_dictionary
- This script can ONLY be tested on
pe15.cs.umb.edu
The script must have 4 functions:
Functions
script_filename
- Header
def script_filename():
- This function returns the filename of the script
hw6.py
- It uses argv from the
sys module
system_var
- Header
def system_var(var):
- This function returns the value of the system variable given by
the parameter var
count_files
- Header
def count_files(path):
- This function goes to the directory specified by the parameter
path
- It then counts all the files (not directories) in that
directory and returns that count
- The function should use the following algorithm.
go to the directory indicated by path
initialize count to 0
create the list entries of all items in the directory
for each entry in the list entries
if entry is a file
increment count
return count
executable_files_dictionary
go to the directory indicated by path
create the empty dictionary files_dict
create the list entries of all items in the directory
for each entry in the list entries
if entry is a file
run split
on entry to get values for name and ext
if ext is contained in the list EXECUTABLE_EXT
if ext is NOT in files_dict
create the list new_list with a name as its only entry
create a new entry in files_dict with key ext and value new_list
else
get the value in files_dict associated with ext and assignment it to files_list
append name to files_list
set the value associated with ext in files_dict to files_list
return files_dict
Test Code
- Your script must have the following test code at the bottom
of the script
FILES_DIR = "/home/ghoffman/course_files/it117_files/misc_scripts"
EXECUTABLE_EXT = ["pl", "py", "sh"]
print(script_filename())
print(system_var("SHELL"))
print(count_files(FILES_DIR))
files_dictionary = executable_files_dictionary(FILES_DIR)
for ext in sorted(files_dictionary):
print(ext, files_dictionary[ext])
- The test code above will looks for the system variable
SHELL and the directory
/home/ghoffman/course_files/it117_files/misc_scripts
- It will ONLY work on pe15.cs.umb.edu
- Yoy may work on the code on your own machine
- But you must move it to pe15.cs.umb.edu
to test it
Output
- Your output should look like this
$ ./hw6.py
hw06.py
/bin/bash
9
pl ['perl_evens.pl']
py ['semester.py', 'regex_test_with_group.py', 'regex_test.py']
sh ['admin_commit.sh', 'advising_log_prune.sh', 'attendance_commit.sh']
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.
Write the headers for all the specified functions specified above.
Under each header write the Python statement pass
.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
script_filename.
In its place write a statement to return the value of
sys.argv.
Copy the test code to the bottom of the script.
Comment out all but the first 3 lines.
Run the script.
You should see
['./hw6.py']
Fix any errors you find.
-
Change the
return
statement so it only returns the first
value in the list.
Run the script.
You should see
./hw6.py
Fix any errors you find.
-
There is a function in the os module that, when run on a
pathname, returns only the filename.
Apply that function to the first value in the list.
Run the script.
You should see
hw6.py
Fix any errors you find.
-
Remove the
pass
statement from
system_var.
There is a dictionary in the os module,
where system variables
are the key and the value is the value of the system variable.
Use that dictionary to return the value of the system variable
contained in the parameter
var.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
count_files.
Replace it with a statement that goes to the directory specified in
the parameter path.
Initialize the variable count to 0.
There is an os module function that creates
a list of entries in a directory.
Use this function to create the list
entries.
Return entries.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
return
statement.
Write a for
loop with loop variable
entry over the list
entries.
Inside the loop, print entry.
Run the script.
It should list and the files witn "None" at the end.
Fix any errors you find.
-
Remove the
print
statement.
There is an os module function that returns
True
if its argument is a file.
Use this function in an if
statement that works
if entry is a file.
Inside the if
statement, print
entry.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that increments
count.
Outside the for
loop, return
count.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
executable_files_dictionary.
Replace it with a statement that goes to the directory specified in
the parameter path.
Create an empty dictionary called files_dict.
Uncomment the next line in the test code.
Run the script.
You should not see any change in the output.
Fix any errors you find.
-
Create the list entries containing everything
in the current directory.
Write a for
loop over entries
using the loop variable entry.
Inside the loop, print entry.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Write an if
statement that works if entry is a file.
Print entry inside the if
statement.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Using split
on entry with an argument of
".", assign values to the variables name and ext.
For an example of what I mean, see
Class Exercise 3.
Print the values of name and ext.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Write an if
statement that works if ext is
in the list EXECUTABLE_EXT.
Inside the if
statement, print entry.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Write an if
statement that works if
ext is not already in the dictionary
files_dict.
Inside this if
statement create the list
new_list whose only element is
entry.
Make a new entry in files_dict using
ext as the key and
new_list as the value.
Outside the for
loop return
files_dict.
Uncomment the last two lines of the test code.
Run the script.
You should see
pl ['perl_evens.pl']
py ['semester.py']
sh ['advising_log_prune.sh']
Fix any errors you find.
-
Add an
else
clause to the if
statement you created above.
Inside the else
clause assign the current value associated with the key
ext to the variable files_list.
Append entry to files_list.
Change the entry in files_dict associated
with ext to
files_list.
Run the script.
You should see
pl ['perl_evens.pl']
py ['semester.py', 'regex_test_with_group.py', 'regex_test.py']
sh ['advising_log_prune.sh', 'attendance_commit.sh', 'admin_commit.sh']h
Fix any errors you find.
Copy the Script to Unix
- 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
Testing the Script on Unix
- Connect to pe15.cs.umb.edu
using an ssh client like putty.exe (Windows)
or ssh (Mac)
- Go to the directory for this exercise
cd it116/hw/hw6
- Run this script
./hw6.py
- You should see
hw6.py
/bin/bash
9
pl ['perl_evens.pl']
py ['semester.py', 'regex_test_with_group.py', 'regex_test.py']
sh ['admin_commit.sh', 'advising_log_prune.sh', 'attendance_commit.sh']
Copyright © 2024 Glenn Hoffman. All rights reserved. May not be reproduced without permission.