Create and write a txt/csv file?

john's Avatar

john

Oct 18, 2016 @ 01:14 AM

Hi Frederik,

Is there any way to create and write to a txt or csv file from inside a custom Python node function?

Python code like this:

def writetemp():
f = open('temp.txt','w')
f.write('hello')
f.close()
return(15)

or this:

import csv
def writetemp():
myarray=[[1],[0],[0],[1],[1]]
f=open('temp.csv','w')
writer=csv.writer(f)
for row in myarray:
writer.writerow(row)
f.close()
return(10)

executes fine from a Python console. When I wrap it in a NodeBox node, the function executes and returns its dummy value with no error messages, but no file is created. Jython claims to support this functionality so I thought it might work.

Is there any way to do this in NodeBox 3?

Thanks!

John

  1. Support Staff 1 Posted by Frederik De Ble... on Oct 18, 2016 @ 07:13 AM

    Frederik De Bleser's Avatar

    I'm assuming it works, but I don't know where it places the file. Maybe you can search for it? Try using an absolute path, or something based off the home directory (os.environ['HOME']).

  2. Support Staff 2 Posted by john on Oct 18, 2016 @ 07:46 AM

    john's Avatar

    Yes! Specifying the absolute path worked. Thank you.

    When I tried before, Spotlight failed to find it anywhere on my hard drive or in my iCloud account. Apparently if no absolute path is specified, the file is not created but no error is generated. os.environ['HOME'] yields "/Users/john", but it was not placed there either - until I added that to the file definition.

    This opens up many possibilities.

    Thanks again,

    John

  3. Support Staff 3 Posted by Frederik De Ble... on Oct 18, 2016 @ 08:02 AM

    Frederik De Bleser's Avatar

    I would find it very weird that the file is not created anywhere. I assume it would be created in the current working directory -- however where that is, I don't know :-)

    A good idea is to look for it from the command line using

    sudo find / -name yourfile.txt
    

    (The sudo is there so it doesn't overload you with errors of directories it can't access)

  4. Support Staff 4 Posted by john on Oct 18, 2016 @ 08:12 AM

    john's Avatar

    I wonder if this is a Mac OS Sierra issue.

    From the console, my non-absolute file landed in my Documents folder (which I would never have noticed if Spotlight hadn't found it). Under Sierra, Documents no longer resides on my hard drive, but is instead moved to iCloud (if you enable that feature, which I did).

    Maybe Jython doesn't yet grok a Documents folder in the cloud.

  5. Support Staff 5 Posted by Frederik De Ble... on Oct 18, 2016 @ 08:15 AM

    Frederik De Bleser's Avatar

    Ah, maybe that's the reason. I don't know which magic they use for saving files immediately in the cloud, though. The developer documentation I can find on this is sparse:

    https://forums.developer.apple.com/thread/51880

    And again, like the UI issues, it is something that is hidden behind the JDK abstraction layer.

Reply to this discussion

Internal reply

        No formatting (switch to Markdown)

          You can attach files up to 10MB

          If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

            Keyboard shortcuts

            Generic

            ? Show this help
            ESC Blurs the current field

            Comment Form

            r Focus the comment reply box
            ^ + ↩ Submit the comment

            You can use Command ⌘ instead of Control ^ on Mac

            Recent Discussions

            21 Apr 10:47
            21 Apr 10:41
            21 Apr 05:56
            20 Apr 10:11
            19 Apr 19:17

             

            18 Apr 00:04
            14 Apr 22:18
            13 Apr 23:09
            10 Apr 23:30
            10 Apr 22:16
            10 Apr 22:02