5

I'm beginner. I try to use a code that works to create a script processing. The code creates folder and subfolder using the value os a column. This is then processing script

import os
##Crea folder and subfolder=name
##Vector=group
##layer=vector
##idx=field layer
##path=folder

#layer = iface.activeLayer()
#idx = layer.fieldNameIndex('NAME')  
#path = "D:\\GIS\\_Temp\\"


for feat in layer.getFeatures():
    attrs = feat.attributes()
    if not os.path.exists(path + attrs[idx]):
        os.makedirs(path + attrs[idx])


for feat in layer.getFeatures():
    attrs = feat.attributes()
    foto=os.path.join(attrs[idx], attrs[idx]+"_foto")
#    foto= attrs[idx]+'\\'+attrs[idx]+'_foto'
    if not os.path.exists(path + foto):
        os.makedirs(path + foto)
    else:
        continue    

for feat in layer.getFeatures():
    attrs = feat.attributes()
    doc= os.path.join(attrs[idx], attrs[idx]+"_documenti")
    if not os.path.exists(path + doc):
        os.makedirs(path + doc)
    else:
        continue       

for feat in layer.getFeatures():
    attrs = feat.attributes()
    vid= os.path.join(attrs[idx], attrs[idx]+"_video")
    if not os.path.exists(path + vid):
        os.makedirs(path + vid)
    else:
        continue

but It returns this error: 'unicode' object has no attribute 'getFeatures' . and I don't understand what the problem is.

3
  • Try changing ##layer=vector to ##Layer=vector. Then before your first for loop, try adding layer = processing.getObject(Layer)
    – Joseph
    Apr 3, 2017 at 10:57
  • Changing ##layer=vector to ##Layer=vector the layer form doesn't appear. Using another name for the variable and adding layer = processing.getObject(Layer) It doesn't work and the error is list indices must be integers, not unicode Apr 3, 2017 at 11:04
  • That error is due to the fieldNameIndex requiring the index of the field instead of the name :)
    – Joseph
    Apr 3, 2017 at 11:33

1 Answer 1

5

Made some tweaks but I tested the first part of your code which worked:

##Create folder and subfolder=name
##Layer=vector
##Field=field Layer
##Path=folder

import os
layer = processing.getObject(Layer)
idx = layer.fieldNameIndex(Field)
path = Path + "/"

for feat in layer.getFeatures():
    attrs = feat.attributes()
    if not os.path.exists(path + attrs[idx]):
        os.makedirs(path + attrs[idx])

You can try adding the rest of your code to the script and see if it works.

4
  • This time: coercing to Unicode: need string or buffer, QPyNullVariant Apr 3, 2017 at 12:08
  • @DanielePiccolo - Did the error occur using the code I posted or when you added the rest of your code?
    – Joseph
    Apr 3, 2017 at 12:12
  • I'm trying with a different shapefile and I think it works. Apr 3, 2017 at 12:24
  • @DanielePiccolo - Most welcome, glad it worked :)
    – Joseph
    Apr 3, 2017 at 12:27

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.