Skip to content

Commit

Permalink
Updated python examples to use with.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthKolekar committed Jan 25, 2016
1 parent 60f7fc5 commit 21e1f1d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
10 changes: 4 additions & 6 deletions examples/add_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ def PromptForAddress(person):

# Read the existing address book.
try:
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
with open(sys.argv[1], "rb") as f:
address_book.ParseFromString(f.read())
except IOError:
print sys.argv[1] + ": File not found. Creating a new file."

# Add an address.
PromptForAddress(address_book.people.add())

# Write the new address book back to disk.
f = open(sys.argv[1], "wb")
f.write(address_book.SerializeToString())
f.close()
with open(sys.argv[1], "wb") as f:
f.write(address_book.SerializeToString())
5 changes: 2 additions & 3 deletions examples/list_people.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def ListPeople(address_book):
address_book = addressbook_pb2.AddressBook()

# Read the existing address book.
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
with open(sys.argv[1], "rb") as f:
address_book.ParseFromString(f.read())

ListPeople(address_book)

0 comments on commit 21e1f1d

Please sign in to comment.