Python Forum
Browse and build a list of folder paths using recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Browse and build a list of folder paths using recursion
#1
I'm trying to browse an OPC server and find the folders in that server recursively and add each folder path to a list.
For example if I have root folders A and B and under thoses folders I have subfolders(1,2,3) the the format would be ['A\1','A\2','A\3','B\1','B\2','B\3'].
I can browse the server and get all the folders under the server but just can't get them in the format I mentioned above.
The code gets the names and node IDs of each folder and then uses the NodeID to browse deeper in the tree. The elementType tells me that its's folder type and any other elementType I don't care about.
Attached is an image of the folder structure under the opc server and a some small output from the code.
ServerName = "LINX-7912 Server"
root = ""



def BrowseServer(server,root):
	#print 'started BrowseServer(%s,%s)' % (server,root)
	path_list = []
	browseServer = system.opc.browseServer(server,root)
	for browse_object in browseServer:
		nodeName = str(browse_object.getDisplayName())
		nodeId = str(browse_object.getServerNodeId().getNodeId())
		elementType = str(browse_object.getElementType())
		if elementType == "OBJECT":
			print nodeName
			path_list.append(nodeName)
			path_list+=BrowseServer(server,nodeId)
			
			
	return path_list

obj_list = BrowseServer(ServerName,root)
print "\n"
for obj in obj_list:
	print obj
[Image: uk2Sb.png]
Reply
#2
In line 17 you do a recursive call to "BrowseServer(server,nodeId)" but you do nothing with the result of that call.
Should this perhaps have to be:
path_list += BrowseServer(server,nodeId)
?
Reply
#3
(Apr-21-2021, 08:43 AM)ibreeden Wrote: In line 17 you do a recursive call to "BrowseServer(server,nodeId)" but you do nothing with the result of that call.
Should this perhaps have to be:
path_list += BrowseServer(server,nodeId)
?

I noticed that and updated the code, but I'm still in the same situation as far as getting the format right take a look at the image:

1. You can see the OPC browse pane towards the left and the folders that I'm retrieving.
2. You can also see the code and output from the code towards the far right.

The output from code gets all the folders and the children under those folders but the issue as said before is formatting that I can't seem to get right?
In the image you can see I highlighted a set of paths based on the folder structure in the OPC browse pane. The code gets to those children folders and outputs them together to show that it went in/out of each folder.
The format for what I highlighted in blue should be:

["Loytec ROOT\BACnet Port\Datapoints\CVS-B230A","Loytec ROOT\BACnet Port\Datapoints\CVS-C220A","Loytec ROOT\BACnet Port\Datapoints\CVS-C220B"]

[Image: uk2Sb.png]
Reply
#4
I am not familiar with Jython, so the objects you are using are a bit foreign to me. But I see you are calling "BrowseServer(server, nodeId)" and it seems to work. So nodeID contains a path. Can you try to change "print(nodeName)" on line 15 to "print(nodeId)"? Does that show a useful path?
Reply
#5
(Apr-22-2021, 01:53 PM)ibreeden Wrote: I am not familiar with Jython, so the objects you are using are a bit foreign to me. But I see you are calling "BrowseServer(server, nodeId)" and it seems to work. So nodeID contains a path. Can you try to change "print(nodeName)" on line 15 to "print(nodeId)"? Does that show a useful path?

There's no need to print the nodeID because it'll be the same outcome, except it'll be a string integer for example: "122344".
We use nodeID to identify the folder we are going into because the nodeName is not enough and that's how the opcServer identifies the different element types within its structure.

You don't have to worry about the objects or jython because I can assure you the objects that are being used are going in/out of the server tree in the right path because originally I used a nested for loop but I could only go so far in the tree but the output was in the correct order as I expected . I just couldn't go deeper in the tree that's what brought me to use recursion? Take a look at the code below:

[Image: CSGUM.png]


The output is what I want, but I need to go deeper in the tree because there's more folders?
You can take a look at the OPC Browse pane in the image towards the left and then you can look under the "bacnet Port" folder and see more folders I need to grab but the nested for loop is not enough?
Reply
#6
Well then there is only one way left: we introduce an extra parameter: "superfolder". What is a superfolder? Well everybody knows what a subfolder is: a folder below the current folder. So a superfolder is the opposite: the folder(path) above.
ServerName = "LINX-7912 Server"
root = ""
 
def BrowseServer(server,root, superfolder = ""):
    #print 'started BrowseServer(%s,%s)' % (server,root)
    path_list = []
    browseServer = system.opc.browseServer(server,root)
    for browse_object in browseServer:
        nodeName = str(browse_object.getDisplayName())
        nodeId = str(browse_object.getServerNodeId().getNodeId())
        elementType = str(browse_object.getElementType())
        if elementType == "OBJECT":
            fullpath = superfolder + "/" + nodeName
            print fullpath
            path_list.append(fullpath)
            path_list += BrowseServer(server, nodeId, fullpath)
    return path_list
 
obj_list = BrowseServer(ServerName,root)
print "\n"
for obj in obj_list:
    print obj
(I could not test it so I hope i made no mistakes)
Reply
#7
  • what does you mean system in this line please explain why you use system keywords here

browseServer = system.opc.browseServer(server,root)
Reply
#8
(Apr-24-2021, 08:29 AM)ibreeden Wrote: Well then there is only one way left: we introduce an extra parameter: "superfolder". What is a superfolder? Well everybody knows what a subfolder is: a folder below the current folder. So a superfolder is the opposite: the folder(path) above.
ServerName = "LINX-7912 Server"
root = ""
 
def BrowseServer(server,root, superfolder = ""):
    #print 'started BrowseServer(%s,%s)' % (server,root)
    path_list = []
    browseServer = system.opc.browseServer(server,root)
    for browse_object in browseServer:
        nodeName = str(browse_object.getDisplayName())
        nodeId = str(browse_object.getServerNodeId().getNodeId())
        elementType = str(browse_object.getElementType())
        if elementType == "OBJECT":
            fullpath = superfolder + "/" + nodeName
            print fullpath
            path_list.append(fullpath)
            path_list += BrowseServer(server, nodeId, fullpath)
    return path_list
 
obj_list = BrowseServer(ServerName,root)
print "\n"
for obj in obj_list:
    print obj
(I could not test it so I hope i made no mistakes)

This seemed to work minus the extra forward slash at the beginning, take a look at the image, once again look at the "OPC Browse" Pane towards the left to see the folder structure of the OPC Server "LINX-7912 Server!
You can see from the new image there're two root folders "Loytec ROOT" and "Server" with that said each OPC server could have a different amount of root folders? I could easily strip the first forward slash, or modify.

[Image: Vkc9I.png]
Reply
#9
(Apr-24-2021, 08:59 AM)Anldra12 Wrote:
  • what does you mean system in this line please explain why you use system keywords here

browseServer = system.opc.browseServer(server,root)

I’m using an IDE called Ignition by inductive automation and the scripting API is under the module system! Ignition utilizes Jython which in short is the python language, that utilizes the Java library, and runs natively on the JVM.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 548 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
Question Keyword to build list from list of objects? pfdjhfuys 3 1,568 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,529 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  pdf2image, poppler and paths jehoshua 18 14,654 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,470 May-29-2022, 09:11 AM
Last Post: snippsat
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,489 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,477 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  automatically get absolute paths oclmedyb 1 2,119 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Using recursion instead of for loops / list comprehension Drone4four 4 3,142 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  chkFile with absolute paths JarredAwesome 7 2,983 Sep-21-2020, 03:51 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020