How can I get all materials which are using an ambient occlusion node?
Not all materials have an associated node_tree. This is only present where the 'use_nodes' checkbox has been used as this creates the node tree.
Your problem is that one or more material does not have a node_tree. The solution is to include a test before the ntree.nodes.get() to first check that there is actually an attribute 'nodes' - and to simply skip that material if none is found:
import bpy

# Run through all materials of the current blend file
for mat in bpy.data.materials:
    # If the material has a node tree
    if mat.node_tree:
        # Run through all nodes
        for node in mat.node_tree.nodes:
            # If the node type is Ambient Occlusion 
            if node.type == 'AMBIENT_OCCLUSION':
                # Print the name of the material
                print(&quot;Ambient Occlusion Node found in:&quot;, mat.name)