What happens after return statement?

Gustaf Liljegren gustafl at algonet.se
Mon Oct 21 05:41:17 CEST 2002


I really ought to know better after 2 years with Python, but I became 
uncertain. Have a look, please:

# Return the contents of a file (strip DOCTYPE conditionally)
def read_file(file, remove):
  if sys.path.exists(file):
    f = open(file, 'r')
    if remove = 0: # Just read the file
      return f.read()
    else: # Read the file and remove any line starting with '<!DOCTYPE'
      sum = ""
      while 1:
        line = f.readline()
        if line = "": break
        if line[:9] != '<!DOCTYPE':
          sum = sum + line
      return sum
    f.close() # Is the file closed (in both cases) here?
  else:
    return ""

Is the file closed, or is it not? That is, what happens after each return 
statement?

# Gustaf



More information about the Python-list mailing list