Commit 21e1f1d2 authored by Parth Kolekar's avatar Parth Kolekar

Updated python examples to use with.

parent 60f7fc51
...@@ -43,9 +43,8 @@ address_book = addressbook_pb2.AddressBook() ...@@ -43,9 +43,8 @@ address_book = addressbook_pb2.AddressBook()
# Read the existing address book. # Read the existing address book.
try: try:
f = open(sys.argv[1], "rb") with open(sys.argv[1], "rb") as f:
address_book.ParseFromString(f.read()) address_book.ParseFromString(f.read())
f.close()
except IOError: except IOError:
print sys.argv[1] + ": File not found. Creating a new file." print sys.argv[1] + ": File not found. Creating a new file."
...@@ -53,6 +52,5 @@ except IOError: ...@@ -53,6 +52,5 @@ except IOError:
PromptForAddress(address_book.people.add()) PromptForAddress(address_book.people.add())
# Write the new address book back to disk. # Write the new address book back to disk.
f = open(sys.argv[1], "wb") with open(sys.argv[1], "wb") as f:
f.write(address_book.SerializeToString()) f.write(address_book.SerializeToString())
f.close()
...@@ -31,8 +31,7 @@ if len(sys.argv) != 2: ...@@ -31,8 +31,7 @@ if len(sys.argv) != 2:
address_book = addressbook_pb2.AddressBook() address_book = addressbook_pb2.AddressBook()
# Read the existing address book. # Read the existing address book.
f = open(sys.argv[1], "rb") with open(sys.argv[1], "rb") as f:
address_book.ParseFromString(f.read()) address_book.ParseFromString(f.read())
f.close()
ListPeople(address_book) ListPeople(address_book)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment