LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Create directory

    Employee
    Posted 12-13-2012 11:36

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: fervex

    Hi again!

    Please clarify how to create a directory via Lavastorm?

    Thanks!


  • 2.  RE: Create directory

    Employee
    Posted 12-17-2012 11:19

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    There isn't currently a way to create a directory in BRAINscript; you would need to use the Java or Python node to do it.

    If you use the Python Filter node, the code would be pretty simple. You would want to add the line:

    import os

    at the top, right under the other import statement, and you could add something like the following (properly indented) as the first line in the handleRecord function:



    os.mkdir(x['dirFieldName'])


    assuming the directory to make is in the field called "dirFieldName'

    Cheers
    Rich


  • 3.  RE: Create directory

    Employee
    Posted 12-18-2012 06:58

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: fervex

    Thanks!
    I have finally solved the problem. here is the result

    import braininfo, os
    
    
    def setup(brainNodeControlObj, BrainNodeClass):
    	''' Must return a class that inherits from BrainNodeClass. '''	
    	true = True  # BRE boolean params expand to lower
    	false = False # case boolean values
    	class BrainNode(BrainNodeClass):
    		def initialize(self):
    			''' Called at node initialization, before first handleRecord.'''
    			super(BrainNode, self).initialize()
    			for output in self.outputs:
    				output.metadata = self.inputMetadata
    
    		def finalize(self, val):
    			''' Called at node end, after last handleRecord call. '''
    			super(BrainNode, self).finalize(val)
    
    		def handleRecord(self, x):
    			for output in self.outputs:
    				p = x["fullpath"]		 # fullpath - is the column name with full paths of directories you want to create
    				if not os.path.exists(p) :   # adding a check for directory existance
    					os.makedirs(p)	 	# creating a directory
    				output.write(x)
    				
    	return BrainNode


  • 4.  RE: Create directory

    Employee
    Posted 12-19-2012 18:04

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    Fervex, I'm not sure I follow. I think you should be able to just put

    os.mkdir(x['Column1'])

    to make that directory.

    So change:
            def handleRecord(self, x):
                for output in self.outputs:
                    output.write(x)
    to

            def handleRecord(self, x):
                os.mkdir(x['Column1'])
                for output in self.outputs:
                    output.write(x)
    in the python filter node, and make sure you add the import os line at the top.


  • 5.  RE: Create directory

    Employee
    Posted 12-19-2012 23:43

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: fervex

    You are right of course.

    In case you have a column with a list of directories to create that doesn't work.
    You can receive a error that either such directory already exests or you can't create directory due to absence of previous level (top level) directory.


  • 6.  RE: Create directory

    Employee
    Posted 12-20-2012 07:09

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    instead of os.mkdir(x['Column1']):

    dirToMake = x['Column1']
    if not os.path.isdir(dirToMake):
    	os.mkdirs(dirToMake)
    NOTE: I changed the mkdir to mkdirs as well, which will make the whole path if necessary.

    Also, you will need to change
    import os
    to
    import os, os.path

    Cheers
    Rich