3

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.

How can i do that.

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?

Python version : 3.5

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.")
Linu
  • 589
  • 1
  • 10
  • 23
  • 2
    Encrypt the file. – Frank C. Jun 11 '20 at 08:05
  • @FrankC.I'm new to python, I want to share this CSV file to a colleague ,But want this to be password protected, If we use encryption then we have to decrypt it as well right? If you can give little bit details will be very helpful. – Linu Jun 11 '20 at 08:10
  • 4
    A CSV file is just a text file, so it has no mechanism to be password protected. The only file types I know of that do are archives (zip, 7z, rar, etc). Python does have a built-in zip module, but it cannot create encrypted zip archives (password-protected archives are encrypted). You could have 7-zip installed and call it as a subprocess to create one, but I can think of no easy way to do this from default Python modules. – Sam Morgan Jun 11 '20 at 08:49
  • I checked in google and didn't find an answer for this, if anyone can help on this please. – Linu Jun 11 '20 at 08:50
  • here they talk about encrypting using AES in python https://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256 – Toerktumlare Jun 11 '20 at 08:56

0 Answers0