How to use `layer.getFeatures()` in a standalone script?

For the first question:

You need to create a QgsVectorLayer object from your Shapefile path. Only then you can access the getFeatures() function. That is:

for fname in glob.glob(path_dir + "*.shp"):
    for feature in QgsVectorLayer(fname,"any name","ogr").getFeatures():

My recommended way of doing it

You don't actually need to access getFeatures() nor to use a for. You can accomplish what you want with this code:

path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

for fname in glob.glob(path_dir + "*.shp"):
    vLayer = QgsVectorLayer(fname,"any name","ogr")
    idx = vLayer.fieldNameIndex('Rank')
    Rank_value = vLayer.uniqueValues(idx)[0] # uV returns a list, get the 1st element

    #Do some processing, finish with field calculator algorithm
    general.runalg("qgis:fieldcalculator", fname, 'Rank', 1, 10, 0, False, Rank_value, path_res + os.path.basename(fname))

Since you'll be using QgsVectorLayer, don't forget to set the prefix path before initializing QGIS, as you did at Getting random values from geometryType() in a standalone PyQGIS script.


I've tested the following script on GNU/Linux, QGIS v.2.8.1, Processing v.2.6 and Processing v.2.9.1.

import sys,os,glob
from qgis.core import *
from PyQt4.QtGui import *

app = QApplication([])
QgsApplication.setPrefixPath("/usr", True) # Adjust it to your path
QgsApplication.initQgis()

path_dir = "/docs/borrar/test/"
path_res = path_dir + "results/"

sys.path.append("/usr/share/qgis/python/plugins/") # Where Processing is installed
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

for fname in glob.glob(path_dir + "*.shp"):
    vLayer = QgsVectorLayer(fname,"any name","ogr")
    idx = vLayer.fieldNameIndex('rank')
    Rank_value = vLayer.uniqueValues(idx)[0] # uV returns a list, get the 1st element

    #Do some processing, finish with field calculator algorithm
    outputs_1=general.runalg("qgis:creategrid",1,"-956314.538361,3285493.72235,-53712.3143749,1885220.75154",250000,250000,None)
    outputs_2=general.runalg("qgis:clip", outputs_1['OUTPUT'], fname, None)
    general.runalg("qgis:advancedpythonfieldcalculator", outputs_2['OUTPUT'], 'rank', 1, 10, 3, False, "value=%f" % Rank_value, path_res + os.path.basename(fname))

QgsApplication.exitQgis()

As you can see, the syntax of the qgis:creategrid algorithm differs from your version and from the version we ran at Import error for qgis.core when running OSGeo4w shell script. Additionally, I ended up using the qgis:advancedpythonfieldcalculator algorithm, which provides a help.

Hope you can make it to work in your environment, pay attention to paths.