OLD | NEW |
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 | 2 |
3 # See README.txt for information and build instructions. | 3 # See README.txt for information and build instructions. |
4 | 4 |
5 import addressbook_pb2 | 5 import addressbook_pb2 |
6 import sys | 6 import sys |
7 | 7 |
8 # Iterates though all people in the AddressBook and prints info about them. | 8 # Iterates though all people in the AddressBook and prints info about them. |
9 def ListPeople(address_book): | 9 def ListPeople(address_book): |
10 for person in address_book.people: | 10 for person in address_book.people: |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 # Main procedure: Reads the entire address book from a file and prints all | 25 # Main procedure: Reads the entire address book from a file and prints all |
26 # the information inside. | 26 # the information inside. |
27 if len(sys.argv) != 2: | 27 if len(sys.argv) != 2: |
28 print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE" | 28 print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE" |
29 sys.exit(-1) | 29 sys.exit(-1) |
30 | 30 |
31 address_book = addressbook_pb2.AddressBook() | 31 address_book = addressbook_pb2.AddressBook() |
32 | 32 |
33 # Read the existing address book. | 33 # Read the existing address book. |
34 f = open(sys.argv[1], "rb") | 34 with open(sys.argv[1], "rb") as f: |
35 address_book.ParseFromString(f.read()) | 35 address_book.ParseFromString(f.read()) |
36 f.close() | |
37 | 36 |
38 ListPeople(address_book) | 37 ListPeople(address_book) |
OLD | NEW |