aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2016-02-11 12:32:42 -0800
committerJoshua Haberman <jhaberman@gmail.com>2016-02-11 12:32:42 -0800
commit635ce854ed159b636076c11ed26755979274b85b (patch)
treed1e80484258476f57bac24cd606ab6b7fc75d636
parentd2c7fe6bc5d28b225f6202684574fe4ef9e3a3a8 (diff)
parent21e1f1d2e99cf194185107fbbd15620b36d99e37 (diff)
downloadprotobuf-635ce854ed159b636076c11ed26755979274b85b.tar.gz
protobuf-635ce854ed159b636076c11ed26755979274b85b.tar.bz2
protobuf-635ce854ed159b636076c11ed26755979274b85b.zip
Merge pull request #1179 from ParthKolekar/python-examples-with
Updated python examples to use with.
-rwxr-xr-xexamples/add_person.py10
-rwxr-xr-xexamples/list_people.py5
2 files changed, 6 insertions, 9 deletions
diff --git a/examples/add_person.py b/examples/add_person.py
index fd81c982..0b698579 100755
--- a/examples/add_person.py
+++ b/examples/add_person.py
@@ -43,9 +43,8 @@ address_book = addressbook_pb2.AddressBook()
# 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."
@@ -53,6 +52,5 @@ except IOError:
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())
diff --git a/examples/list_people.py b/examples/list_people.py
index 755de901..f131872d 100755
--- a/examples/list_people.py
+++ b/examples/list_people.py
@@ -31,8 +31,7 @@ if len(sys.argv) != 2:
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)