Closing a file before or after return?

Gustaf Liljegren gustafl at algonet.se
Fri Feb 9 14:58:39 CET 2001


I'm using Python 2.0.

Have a look at this function. It checks if a particular user (mail adress)
exist in a .htpasswd file. The user name is what comes before ':' on each
line.

# Check if user exists
def user_exist(user):
  f = open('.htpasswd', 'r')
  for line in f.readlines():
    i = string.find(line, ':')
    if line[:i] == user:
      return 1

I'd like to close the file too, but I don't know where to put that
statement. Of course, the file should be closed in both cases -- not just
when the user is found.

Regards,

Gustaf Liljegren





More information about the Python-list mailing list