Write to file

from System.IO import Path, StreamWriter
from Spotfire.Dxp.Application.Visuals import CrossTablePlot

#targetFileName = "\\\\company.com\\your\\folder\\test.txt"
#targetFileName = "C:\\temp\\test.txt
targetFileName = Path.GetTempFileName()


#Write to file 
writer = StreamWriter(targetFileName,True) #True to append
aLine = "|".join(("str1","str2","str3"))
writer.Write(aLine)
writer.Close()

#Debug
print targetFileName
print aLine


Note: If using writing to a network folder from Webplayer, make sure the folder has proper permissions for the service account running Webplayer. To check what service account webplayer is using, add these lines to output the result in a Text Area Spotfire Label Control.

#Include dependencies
from System.Security.Principal import NTAccount
from System.IO.File import GetAccessControl

#get owner
owner = GetAccessControl(targetFileName).GetOwner(NTAccount);

#store owner in an existing "whoami" string Document Property  
Document.Properties["whoami"] = str(owner)

5 comments:

The Alexu Boy said...

Is there any way I can read from a file? Using a similar script?

Jose Leviaguirre said...
This comment has been removed by the author.
Jose Leviaguirre said...

checkout this post that writes and reads a file

http://spotfired.blogspot.com/2014/10/cross-table-to-html.html

S Saha said...

Hi Jose,

I'm trying to get input from the users through the input fiekd ( e.g. Text Area -> Property Control -> Input field multiple line) and then write it to a file as you mentioned above.

Can you please help me how to do that? In fact how to assign a value to str1 in the above programming from " Input field".

Thanks,
S Saha

Jose Leviaguirre said...

Hello Sasha,

I guess you could replace the following line:

aLine = "|".join(("str1","str2","str3"))

with:

aLine = Document.Properties["yourMultilineDocPropHere"]