Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Created June 10, 2013 18:01
Show Gist options
  • Select an option

  • Save baroquebobcat/5750890 to your computer and use it in GitHub Desktop.

Select an option

Save baroquebobcat/5750890 to your computer and use it in GitHub Desktop.
With as impl w/ for
# Why is there a with construct, when you can build it with
# for loop and yield?
#
# ;)
def open_f(filename, mode):
try:
f = open(filename, mode)
yield f
finally:
try:
f.close()
except:
pass
def read_lines(filename):
for f in open_f(filename, 'r'):
for line in f:
yield line
for line in read_lines('testfile'):
print("line: " + line.rstrip("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment