tag:support.nodebox.net,2012-11-01:/discussions/nodebox-2-3/5826-create-and-write-a-txtcsv-fileNodeBox: Discussion 2016-10-18T08:15:28Ztag:support.nodebox.net,2012-11-01:Comment/410139322016-10-18T01:14:15Z2016-10-18T01:14:15ZCreate and write a txt/csv file?<div><p>Hi Frederik,</p>
<p>Is there any way to create and write to a txt or csv file from
inside a custom Python node function?</p>
<p>Python code like this:</p>
<p>def writetemp():<br>
f = open('temp.txt','w') f.write('hello') f.close() return(15)</p>
<p>or this:</p>
<p>import csv<br>
def writetemp():<br>
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)</p>
<p>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.</p>
<p>Is there any way to do this in NodeBox 3?</p>
<p>Thanks!</p>
<p>John</p></div>johntag:support.nodebox.net,2012-11-01:Comment/410139322016-10-18T07:13:06Z2016-10-18T07:13:06ZCreate and write a txt/csv file?<div><p>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
(<code>os.environ['HOME']</code>).</p></div>Frederik De Blesertag:support.nodebox.net,2012-11-01:Comment/410139322016-10-18T07:46:25Z2016-10-18T07:46:25ZCreate and write a txt/csv file?<div><p>Yes! Specifying the absolute path worked. Thank you.</p>
<p>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.</p>
<p>This opens up many possibilities.</p>
<p>Thanks again,</p>
<p>John</p></div>johntag:support.nodebox.net,2012-11-01:Comment/410139322016-10-18T08:02:01Z2016-10-18T08:02:01ZCreate and write a txt/csv file?<div><p>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 :-)</p>
<p>A good idea is to look for it from the command line using</p>
<pre>
<code>sudo find / -name yourfile.txt</code>
</pre>
<p>(The <code>sudo</code> is there so it doesn't overload you with
errors of directories it can't access)</p></div>Frederik De Blesertag:support.nodebox.net,2012-11-01:Comment/410139322016-10-18T08:12:03Z2016-10-18T08:12:03ZCreate and write a txt/csv file?<div><p>I wonder if this is a Mac OS Sierra issue.</p>
<p>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).</p>
<p>Maybe Jython doesn't yet grok a Documents folder in the
cloud.</p></div>johntag:support.nodebox.net,2012-11-01:Comment/410139322016-10-18T08:15:28Z2016-10-18T08:15:28ZCreate and write a txt/csv file?<div><p>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:</p>
<p><a href="https://forums.developer.apple.com/thread/51880">https://forums.developer.apple.com/thread/51880</a></p>
<p>And again, like the UI issues, it is something that is hidden
behind the JDK abstraction layer.</p></div>Frederik De Bleser