Introduction to Python
davidbpython.com
Project Discussion, Session 8
| 8.1 | Notes typing assignment. Please write out this week's transcription notes. |
| 8.2 | Function get_file_sizes(), which takes a directory name as an argument and returns a dictionary of each file's name paired with its size in bytes. |
|
|
|
Please let me know your questions! |
|
| 8.3 | Directory Grep. |
|
The program does require a loop within a loop -- the outer loop reads the name of each file in a directory; the inner loop reads each line in each file: Note: this does not need to use a function. The program takes command-line arguments. |
|
# read command-line arguments for directory and search term
# list the files in the directory
# loop through each file in listing of files in directory
# skip if the filename starts with a '.'
# skip if the file is actually a directory
# open the file
# set a counter to 0
# loop through each line in the file
# if the search term is in the line,
# print the filename, line number, file line
|
|
|
|
|
| 8.4 | A watched directory. The purpose of the assignment is to reinforce file lists and use sets to notice differences. Since set.difference() can tell us what's in one set that isn't in the other, it would be pretty simple to be able to say whether a file was added or removed by comparing the contents of the directory, listed out at different times. |
|
The overall approach contemplated here is to list the files in the directory and store them in a set; wait 5 seconds; then list the files into a set again, and compare the sets. You'll do oldset.difference(newset) to detect file deletions and newset.difference(oldset) to detect file additions. Putting this process into a while(True) loop allows this process to go on indefinitely. Since the while(True) loop has no natural way to break out, the program requires the use of Ctrl-C to exit. The logic of the while(True) loop can be done in different order, perhaps, but here is the logic outline that worked for me: |
|
# test sys.argv to affirm that dir argument has been passed
# list files into set ("old" set)
# start while True: loop
# sleep 5 seconds
# list files into set ("new" set)
# compare sets to see what files have been added
# compare sets to see what files have been removed
# report any added or removed files
# assign "new" set to "old" set variable for next loop
comparison
|
|
|
Note that if Python can't find your directory, it may be because the relative path is incorrect. Please see "Filepaths for Locating Files" in Session 3 Slides. |
|
| 8.5 | Largest files in a directory tree using os.walk. This script will be pretty close to the os.walk example in the slides. You simply need to join the directory to the filename to get the filepath, and then use os.path.getsize to get the file's size. Initialize an empty dictionary before the loop begins, store the filepath as the key and size as the value in the dictionary, and after the loop ends, sort the dictionary by value using key=dictname.get (where dictname is the name of your dictionary). Limit the results by using a slice subscript which can be placed directly after sorted(). |
|
Note that if Python can't find your file, it may be because the relative path is incorrect. Please see "Filepaths for Locating Files" in Session 3 Slides. |
|
| 8.6 | (Extra Credit / Supplementary) Use try/except to detect error conditions. |
|
In the "directory grep" assignment, there are two error conditions that must be anticipated: 1. if the user doesn't enter two arguments at the command line 2. if the user enters a directory that doesn't exist. Both of these error conditions can be detected through 'if' tests. But they can also be trapped as exceptions. For example, what exception would occur if the user had not entered 2 arguments and you attempted to read them from sys.argv? And what exception would occur if the program attempted to read from a non-existent directory? To find out, take out your tests that check to see if you have enough arguments (if you have one). Then run it with not enough arguments - one argument or no argument. Note the exception type that you see, and the line it occurs on. Then wrap that line (and the other line reading from sys.argv) in a try: block, and follow that with an except: block. Inside the except: block print an error message and exit. Do the same thing for a bad directory -- deliberately feed a bad directory to the program, note the exception type and where it occurred, and then wrap that line in a try:. |
|