import gzip
import json
from datetime import datetime

def log_database(conn, param, email):
  param = json.dumps(param)
  email_gz = gzip.compress(email.encode('ascii'))
  values = (param, email_gz)

  c = conn.cursor()
  c.execute('''
    INSERT INTO email_log (`param`, `email_gz`)
    VALUES (?, ?)
    ''', values)

def log_text(fname, param, email):
  from datetime import datetime

  f = open(fname, 'a')
  f.write("=== %s ===\n" % datetime.now())
  f.write("args: %s\n" % (" ".join(param),))
  f.write("-------------\n")
  f.write(email)
  f.write("\n\n")
  f.flush()
  f.close()


import gzip
import json
from datetime import datetime

def log_database(conn, param, email):
  param = json.dumps(param)
  email_gz = gzip.compress(email.encode('ascii'))
  values = (param, email_gz)

  c = conn.cursor()
  c.execute('''
    INSERT INTO email_log (`param`, `email_gz`)
    VALUES (?, ?)
    ''', values)

def log_text(fname, param, email):
  from datetime import datetime

  with open(fname, 'a') as f:
    f.write("=== %s ===\n" % datetime.now())
    f.write("args: %s\n" % (" ".join(param),))
    f.write("-------------\n")
    f.write(email)
    f.write("\n\n")
    f.flush()


Use with statement when writing to a file
Use with statement when writing to a file

  import gzip
  import json
  from datetime import datetime
  
  def log_database(conn, param, email):
    param = json.dumps(param)
    email_gz = gzip.compress(email.encode('ascii'))
    values = (param, email_gz)
  
    c = conn.cursor()
    c.execute('''
      INSERT INTO email_log (`param`, `email_gz`)
      VALUES (?, ?)
      ''', values)
  
  def log_text(fname, param, email):
    from datetime import datetime
  
-   f = open(fname, 'a')
+   with open(fname, 'a') as f:
-   f.write("=== %s ===\n" % datetime.now())
+     f.write("=== %s ===\n" % datetime.now())
-   f.write("args: %s\n" % (" ".join(param),))
+     f.write("args: %s\n" % (" ".join(param),))
-   f.write("-------------\n")
+     f.write("-------------\n")
-   f.write(email)
+     f.write(email)
-   f.write("\n\n")
+     f.write("\n\n")
-   f.flush()
+     f.flush()
-   f.close()
  
  
Use with statement when writing to a file
## Code Before:
import gzip
import json
from datetime import datetime

def log_database(conn, param, email):
  param = json.dumps(param)
  email_gz = gzip.compress(email.encode('ascii'))
  values = (param, email_gz)

  c = conn.cursor()
  c.execute('''
    INSERT INTO email_log (`param`, `email_gz`)
    VALUES (?, ?)
    ''', values)

def log_text(fname, param, email):
  from datetime import datetime

  f = open(fname, 'a')
  f.write("=== %s ===\n" % datetime.now())
  f.write("args: %s\n" % (" ".join(param),))
  f.write("-------------\n")
  f.write(email)
  f.write("\n\n")
  f.flush()
  f.close()


## Instruction:
Use with statement when writing to a file
## Code After:
import gzip
import json
from datetime import datetime

def log_database(conn, param, email):
  param = json.dumps(param)
  email_gz = gzip.compress(email.encode('ascii'))
  values = (param, email_gz)

  c = conn.cursor()
  c.execute('''
    INSERT INTO email_log (`param`, `email_gz`)
    VALUES (?, ?)
    ''', values)

def log_text(fname, param, email):
  from datetime import datetime

  with open(fname, 'a') as f:
    f.write("=== %s ===\n" % datetime.now())
    f.write("args: %s\n" % (" ".join(param),))
    f.write("-------------\n")
    f.write(email)
    f.write("\n\n")
    f.flush()

