IT 117: Introduction to Scripting
Homework 6

Due

Sunday, March 3rd at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

The script must have 4 functions:

Functions

script_filename

system_var

count_files

	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

Output

Suggestions


  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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.

Testing the Script on Unix

Copyright © 2024 Glenn Hoffman. All rights reserved. May not be reproduced without permission.