TypeError: an integer is required

Lutfi Oduncuoglu lutfioduncuoglu at gmail.com
Sun Nov 22 12:59:29 EST 2009


Hello,

I am a newbie on oython and I am taking the error at subject my code is
below, I am trying to develop a qgis plugin and lines begin with # is the
thing that I tried. Thus sys.stdout gives the type error. When I comment
that line it turns an error like below. What may be the problem? thanks for
help:)

...\ReadData.py", line 128, in run
    print "%d %s" %(k, attr.toString())
IOError: [Errno 9] Bad file descriptor




# Import the PyQt and QGIS libraries
from PyQt4 import QtCore, QtGui

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from os import *
from qgis.gui import *
import sys
import pdb
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from ReadDataDialog import ReadDataDialog

class ReadData:

  def __init__(self, iface):
    # Save reference to the QGIS interface
    self.iface = iface

  def initGui(self):
    # Create action that will start plugin configuration
    self.action = QAction(QIcon(":/plugins/readdata/icon.png"), \
        "Read shp for calculations", self.iface.mainWindow())
    # connect the action to the run method
    QObject.connect(self.action, SIGNAL("triggered()"), self.run)

    # Add toolbar button and menu item
    self.iface.addToolBarIcon(self.action)
    self.iface.addPluginToMenu("&Read shp for calculations", self.action)

  def unload(self):
    # Remove the plugin menu item and icon
    self.iface.removePluginMenu("&Read shp for calculations",self.action)
    self.iface.removeToolBarIcon(self.action)

  # run method that performs all the real work
  def run(self):

#    fileName =
QFileDialog.getOpenFileName(None,QString.fromLocal8Bit("Select a file:"),"",
"*.shp *.gml")

#    if fileName.isNull():

#      QMessageBox.information(None, "Cancel", "File selection canceled")




#    else:

#      print fileName


    vlayer =
QgsVectorLayer("C:\\Users\\lutfi\\Documents\\tezzzz\\deneme2\\ownership.shp",
"hebe", "ogr")
    print vlayer.source()
    print vlayer.featureCount()
    QgsMapLayerRegistry.instance().addMapLayer(vlayer)
    QMessageBox.information(self.iface.mainWindow(), "info", "file:
"+str(vlayer.source())+" is added.")





    if not vlayer.isValid():
      print "Couldn't open the layer"
      pdb.set_trace()

    else:

#    QMessageBox.information(None, "Cancel", "File selection canceled")

      provider = vlayer.dataProvider()
      feat = QgsFeature()
      allAttrs = provider.attributeIndexes()
      provider.select(allAttrs)

      while provider.nextFeature(feat):
        geom = feat.geometry()
        import sys
        import os
#
win32api.SetFileAttributes('C://Users//lutfi//Documents//tezzzz//log.txt',
win32con.FILE_ATTRIBUTE_NORMAL)
#        sys.stdout = open('C://Users//lutfi//Documents//tezzzz//log.txt',
777 )
        print geom
#        QMessageBox.information(None, "Cancel", "File selection canceled")
        print "Feature ID %d: " % feat.id()
        if geom.type() == QGis.Point:
          x = geom.asPoint()
          print "Point: " + str(x)

        elif geom.type() == QGis.Line:
          x = geom.asPolyline()
          print "Line: %d points" % len(x)

        elif geom.type() == QGis.Polygon:
          x = geom.asPolygon()
          numPts = 0
          for ring in x:
            numPts += len(ring)
            print "Polygon: %d rings with %d points" % (len(x), numPts)
        else:
          print "Unknown"

        attrs = feat.attributeMap()

        for (k,attr) in attrs.iteritems():
          sys.stdout =
os.open("C://Users//lutfi//Documents//tezzzz//log.txt" , "a" )
          print "%d %s" %(k, attr.toString())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091122/baef556d/attachment.html>


More information about the Python-list mailing list