We recommend switching to the latest versions of Edge, Firefox, Chrome or Safari. Using Internet Explorer will result in a loss of website functionality.

Python... create directory?

Comments

7 comments

  • Avatar
    Adrian Williams

    Pretty much all of the script should be retained for the node to operate correctly when you are using it to access input data and/or output data at the node's pins.

    You can find additional information on the use of the Python node in the Python Node Getting Started.pdf document which can be found in the 'docs' directory of your LAE install. e.g. C:\Program Files\Lavastorm\LAE6.1\docs

    The os.path.exists() operator does not discriminate between a file and a directory. If you want to check that the target is a directory and it exists then use os.path.isdir() instead.

    The os.chmod() operator can be used to modify the permissions on the directory.

    See the attached example script for some examples. Note, the os.rmdir() operator will only remove an empty directory.

     

    Attached files

    Python_Node_Create_Directory_and_chmod_script--share.txt

     

    1
    Comment actions Permalink
  • Avatar
    Adrian Williams

    Note, unlike most other output nodes the Output BRD node can create the directory if it does not already exist.

    See also the Help documentation for the output-related scripting operators that can be used to test for the existence of a file, create, delete, clear and move a file.

    0
    Comment actions Permalink
  • Avatar
    Geoff

    ok, thanks Adrian! I tried and tried and it kept complaining about indents.. i was like what?

    but it really is true! LOL you have to line them up!

    I got it working now!

    so if I change exists to isdir this will work the same? (should not matter really because im only passing in directory)

    if not os.path.exists(PTH+"Clean"):
    os.makedirs(PTH+"Clean")
    os.chmod(PTH+"Clean",0o777)
    if not os.path.exists(PTH+"Fallout"):
    os.makedirs(PTH+"Fallout")
    os.chmod(PTH+"Fallout",0o777)

     

    I tried that BRD trick before and it was not very clean (and left a directory I could not delete)

    THANK YOU!

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    Yes that should work.

     

    Python relies on indentation to structure the code blocks within a script. The default script in the node uses tabs. However, when new lines are entered they use multiple space characters for indentation. I find it is simplest to copy the default script into Notepad++ and then use the replace function to change all the tab characters (\t) to be four spaces, and then paste the script back into the node.

    0
    Comment actions Permalink
  • Avatar
    Geoff

    ok cool.

     

    can python set the graph parameters? I have one called FolderName.. id love to be able to read a directory, then create a sub directory using a date (stripped out of a filename)

    then set the param so it can be used later

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    Python can read the graph parameter values but they cannot be written (they are effectively treated as constants when the graph is run).

    You can access the values within the script using textual substitution of the parameter.

    So if you have a graph parameter named 'FolderName' and it has a value set to be '/home/Bob/data' (without the apostrophes) then your script could access the value using the usual textual substitution mechanism, e.g.

    fName = r"{{^FolderName^}}"

     

    which would result in the 'fName' variable having a value of  "/home/Bob/data"

    Note the presence of the r before the opening quote character - this is useful if you are dealing with a mix of Linux and Windows style paths as the backslash characters in the Windows paths would result in errors. It is best to replace any backslash characters with forward slash characters if possible e.g. using 

    fName = fName.replace("\\", "/")

     

    Values resulting from textual substitution should be considered as strings - you can then, if required, cast them to an appropriate data type within your script using the relevant Python operator (e.g. float() etc).

    See the Parameters Overview topic in the Help for additional information on textual substitution.

    0
    Comment actions Permalink
  • Avatar
    Geoff

    ahh.. too bad. that couldve been handy.

    thanks for the r" tip though!

    0
    Comment actions Permalink

Please sign in to leave a comment.