1

My plugin does some basic geoprocessing functionalities and write the output as csv file. What i want is when i open some already writed output csv file in a writable mode and try to replace the same csv file for the newoutput, i got an error which is attached below. Code is also attached for reference.

enter image description here

def writeToFile(self, lsOfCities, filePath):
    fileCSV = open(filePath, "w")        

    for i in lsOfCities:
        fileCSV.write(i)
        fileCSV.write(",")
        fileCSV.write("\n")
    QMessageBox.information(self.dlg, "Number of Cities Near", str(len(lsOfCities)) + " cities are written to "+filePath)
    fileCSV.close()
    self.dlg.close()

I want my plugin to show a warning message instead of below error.

1 Answer 1

3

You need to use a try/except block:

def writeToFile(self, lsOfCities, filePath):
    try:
        fileCSV = open(filePath, "w")

        for i in lsOfCities:
            fileCSV.write(i)
            fileCSV.write(",")
            fileCSV.write("\n")
        QMessageBox.information(self.dlg, "Number of Cities Near", str(len(lsOfCities)) + " cities are written to "+filePath)
        fileCSV.close()
        self.dlg.close()
    except IOError:
        # Add your error reporting/handling code here
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.