8

Working in QGIS I have a point layer with ad attribute "NAME". Is there any way to automatically create a folder called "NAME" where putting the attachments? Maybe I could use something like this "How to check if a directory exists and create it if necessary?" But how could I checking it directly from the active layer in QGIS?

4
  • Could you describe what you mean by "attachment"? And your attribute, is this a field called "NAME"?
    – Joseph
    Commented Jan 18, 2017 at 14:06
  • Attachments are files (pdf, tif ...) that describe the point (Building). "NAME" is the code of building. Commented Jan 18, 2017 at 14:21
  • 1
    What storage format are you using? If the files are in a separate folder, that's not really an "attachment", since it's not actually attached.
    – jpmc26
    Commented Jan 18, 2017 at 17:06
  • Right. I used a wrong word. I mean all the documentation about the building. Commented Jan 18, 2017 at 17:32

1 Answer 1

9

You could try using the following in the Python Console which:

  • Reads all attributes in your "NAME" field
  • Checks the parent directory to see if it contains directories with the same name as the attributes
  • If a directory does not exist, it is created.

    import os
    
    layer = iface.activeLayer()
    idx = layer.fieldNameIndex('NAME')  
    path = "path/to/directory"
    
    for feat in layer.getFeatures():
        attrs = feat.attributes()
        if not os.path.exists(path + attrs[idx]):
            os.makedirs(path + attrs[idx])
    
1
  • 1
    @DanielePiccolo - Most welcome, glad it worked :)
    – Joseph
    Commented Jan 18, 2017 at 15:14

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.