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 # (use try/except to trap exceptions for missing argument and bad 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. |
|