H a n d s o n, p r o j e c t b a s e d
Working with Multiple Files
Download 4.21 Mb. Pdf ko'rish
|
Python Crash Course, 2nd Edition
Working with Multiple Files
Let’s add more books to analyze. But before we do, let’s move the bulk of this program to a function called count_words() . By doing so, it will be easier to run the analysis for multiple books: word_count.py def count_words(filename): u """Count the approximate number of words in a file.""" try: with open(filename, encoding='utf-8') as f: contents = f.read() except FileNotFoundError: print(f"Sorry, the file {filename} does not exist.") else: words = contents.split() num_words = len(words) print(f"The file {filename} has about {num_words} words.") filename = 'alice.txt' count_words(filename) Most of this code is unchanged. We simply indented it and moved it into the body of count_words() . It’s a good habit to keep comments up to date when you’re modifying a program, so we changed the comment to a doc- string and reworded it slightly u. 200 Chapter 10 Now we can write a simple loop to count the words in any text we want to analyze. We do this by storing the names of the files we want to analyze in a list, and then we call count_words() for each file in the list. We’ll try to count the words for Alice in Wonderland, Siddhartha, Moby Dick, and Little Women, which are all available in the public domain. I’ve intentionally left siddhartha.txt out of the directory containing word_count.py, so we can see how well our program handles a missing file: def count_words(filename): --snip-- filenames = ['alice.txt', 'siddhartha.txt', 'moby_dick.txt', 'little_women.txt'] for filename in filenames: count_words(filename) The missing siddhartha.txt file has no effect on the rest of the program’s execution: The file alice.txt has about 29465 words. Sorry, the file siddhartha.txt does not exist. The file moby_dick.txt has about 215830 words. The file little_women.txt has about 189079 words. Using the try - except block in this example provides two significant advantages. We prevent our users from seeing a traceback, and we let the program continue analyzing the texts it’s able to find. If we don’t catch the FileNotFoundError that siddhartha.txt raised, the user would see a full traceback, and the program would stop running after trying to analyze Siddhartha. It would never analyze Moby Dick or Little Women. Download 4.21 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling