0

I am trying to export the data from the table to txt file. I have code as follows

class Optimization_goolge(object):
    def __init__(self):
        self.dbname = ""
        self.usr_name = ""
        self.usr_pass = ""
        self.con = ""
        self.cur = ""
        self.openTime=""

    def vT(self, x, y, z, openTime):
        try:
            con_string="dbname = '%s' user = '%s' password ='%s' host = 'localhost'"%(x,y,z)
            con = psycopg2.connect(con_string)
            con.set_isolation_level(0)
            cur = con.cursor()

            cur.execute(("""COPY (select source, target, sum(cost)/1000 as cost from distance_matrix where source != 88888888 and target
             != 88888888 group by source, target order by source) TO '%s\\vrp_distance.txt'""") % (os.getcwd()))
            con.commit()

But I am getting error

error vT Function could not open file "D:\working_copy\vrp_distance.txt" for writing: No such file or directory

Error optimizeFxn_google arguments did not match any overloaded call:
  QMessageBox.warning(QWidget, QString, QString, QMessageBox.StandardButtons buttons=QMessageBox.Ok, QMessageBox.StandardButton defaultButton=QMessageBox.NoButton): argument 1 has unexpected type 'Optimization_goolge'
  QMessageBox.warning(QWidget, QString, QString, int, int, int button2=0): argument 1 has unexpected type 'Optimization_goolge'
  QMessageBox.warning(QWidget, QString, QString, QString, QString button1Text=QString(), QString button2Text=QString(), int defaultButtonNumber=0, int escapeButtonNumber=-1): argument 1 has unexpected type 'Optimization_goolge'
user1687891
  • 570
  • 4
  • 22
  • As it comes from `getcwd()` I guess the directory exists. Do you have writing permissions? Moreover, you should use `os.path.join()` to join paths instead of your manual way. You can additionally use `.format()` to increase readability. – mikuszefski Jun 07 '18 at 08:27
  • @mikuszefski yes directory and file do exist. how to use format(). can you give me an example – user1687891 Jun 07 '18 at 08:32
  • have a look in the answer below – mikuszefski Jun 07 '18 at 11:27

1 Answers1

2

Try:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, 'vrp_distance.txt')

sql_statement = ("COPY (select source, target, sum(cost)/1000" 
" as cost from distance_matrix where source != 88888888 and target"
" != 88888888 group by source, target order by source) TO"
" '{}'".format(filepath))

Sidenotes to consider (not in original question):

  • why not separate responsibilities to extract information and write it to file - you will knwo for sure which part fails
  • OptimizationGoolge, by convention
  • if you init a class with null strings, something is wrong in class design
  • rename vT to somehting meaningful

You have to check too:

assert os.path.exists('D:/working_copy/vrp_distance.txt') 
assert os.path.exists('D:/working_copy')

Also check Save PL/pgSQL output from PostgreSQL to a CSV file. If you do not have a writing permission, all of the above is fruitless.

Evgeny
  • 3,178
  • 2
  • 11
  • 33