I need to read a remote file using FTP and process it and insert the processed data to a PostgreSQL database in another server.
The below code is working fine, but i want some advice to modify few things.
In this line i don't want to write the actual details, I want that to be hidden from my script. How can i do that?
ftp = FTP('server.com','user_name','password')
The script downloads the file to my local system and process it as it is a local file. Is there any way to read and process it without downloading it to the source server (there might be permission issues also)?
- If there is no way rather than downloading it to the source, should I specify a path where this file would be downloaded rather than the way I have written it?
- Is there anything which i can do to improve this code?
Any guidance will be of great help.
import psycopg2 import time import os #import MySQLdb #import paramiko from ftplib import FTP #from utils.config import Configuration as Config #from utils.utils import get_global_config start_time = time.perf_counter() cnx_psql = psycopg2.connect(host="localhost", database="postgres", user="postgres", password="postgres", port="5432") # Cursors initializations cur_psql = cnx_psql.cursor() def getFile(ftp, filename): try: local_filename = os.path.join(r"/Users/linu/Downloads/", filename) lf = open(local_filename, "wb") ftp.retrbinary("RETR " + filename ,lf.write) print("file copied") except: print ("Error") try: filePath='''/Users/linu/Downloads/log''' #filePath='''/cmd/log/stk/log.txt''' table='staging.stock_dump' SQL="""DROP TABLE IF EXISTS """+ table + """;CREATE TABLE IF NOT EXISTS """+ table + """ (created_date TEXT, product_sku TEXT, previous_stock TEXT, current_stock TEXT );""" cur_psql.execute(SQL) cnx_psql.commit() ftp = FTP('server.com','user_name','password') print("FTP connection succesful") data = [] ftp.cwd('/stockitem/') getFile(ftp,'log') read_file = open(filePath, "r") my_file_data = read_file.readlines() for line in my_file_data: if 'Stock:' in line: fields=line.split(" ") date_part1=fields[0] date_part2=fields[1][:-1] sku=fields[3] prev_stock=fields[5] current_stock=fields[7] if prev_stock.strip()==current_stock.strip(): continue else: cur_psql.execute("insert into " + table+"(created_date, product_sku, previous_stock , current_stock)" + " select CAST('" + date_part1+ " "+ date_part2 + "' AS TEXT)" +", CAST('"+sku+"' AS TEXT),CAST('" + prev_stock +"' AS TEXT),CAST('" +current_stock + "' AS TEXT);") cnx_psql.commit() cur_psql.close() cnx_psql.close() print("Data loaded to DWH from text file") print("Data porting took %s seconds to finish---" % (time.perf_counter() - start_time)) except (Exception, psycopg2.Error) as error: print ("Error while fetching data from PostgreSQL", error) print("Error adding information.") quit() finally: ftp.close()