title: Make a password protected CSV file using Python
body: <p>I have a script to generate a CSV file using the data from Postgres table.
  The script is working fine but i need to make this generated CSV file a password protected one.</p>
  
  <p>How can i do that.</p>
  
  <p>Below is the PY script used to generate the CSV file. What adjustments i have to make to make to password protect the generated CSV file?</p>
  
  <p>Python version : 3.5</p>
  
  <pre><code>import csv
  import os
  import psycopg2
  
  # File path and name.
  filePath = '/Users/admin/documents/'
  fileName = 'details.csv'
  
  # Database connection variable.
  connect = None
  
  # Check if the file path exists.
  if os.path.exists(filePath):
  
      try:
  
          # Connect to database.
          connect = psycopg2.connect(host='localhost', database='postgres',
                                     user='postgres', password='postgres',port='5432')
  
      except psycopg2.DatabaseError as e:
  
          # Confirm unsuccessful connection and stop program execution.
          print("Database connection unsuccessful.")
          quit()
  
      # Cursor to execute query.
      cursor = connect.cursor()
  
      # SQL to select data from the table.
      sqlSelect = "SELECT * FROM details"
  
      try:
  
          # Execute query.
          cursor.execute(sqlSelect)
  
          # Fetch the data returned.
          results = cursor.fetchall()
  
          # Extract the table headers.
          headers = [i[0] for i in cursor.description]
  
          # Open CSV file for writing.
          csvFile = csv.writer(open(filePath + fileName, 'w', newline=''),
                               delimiter=',', lineterminator='\r\n',
                               quoting=csv.QUOTE_ALL, escapechar='\\')
  
          # Add the headers and data to the CSV file.
          csvFile.writerow(headers)
          csvFile.writerows(results)
  
          # Message stating export successful.
          print("Data export successful.")
  
      except psycopg2.DatabaseError as e:
  
          # Message stating export unsuccessful.
          print("Data export unsuccessful.")
          quit()
  
      finally:
  
          # Close database connection.
          connect.close()
  
  else:
  
      # Message stating file path does not exist.
      print("File path does not exist.")
  </code></pre>