IronPython: Export Table to STDF

I've written an Action Control script which exports a Table visualization to an STDF (Spotfire Text Data Format) file. It exports correctly, but at the end the "lock" on the file (presumably from the FileStream object) is not released. So if the user runs the script and exports to a file, and then runs the script again, they get an error if they try to overwrite the file. In the debug window, this is the error:

The process cannot access the file 'C:\Users\ellisr2\Desktop\test.stdf' because it is being used by another process.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

...

Because the file is still locked, the user also can't delete, move, or rename an exported file because Windows also reports that the "file is open in TIBCO Spotfire". The only way to release the lock is to completely exit the Spotfire client. (Closing the DXP file that contains this script doesn't do it.)

My script is pasted below. I am disposing of the FileStream object, which from what I have read is all I'm supposed to have to do. I tried replacing Dispose with separate Flush and Close calls, but that made no difference.

Can someone tell me what is wrong with my script?

-Rich

# ExportTableToSTDF.py

#

# INPUT PARAMETERS

# vizTable : Visualization

import System

from System.IO import FileStream, FileMode

from Spotfire.Dxp.Application.Visuals import TablePlot

from  Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers

import clr

clr.AddReference("System.Windows.Forms")

from System.Windows.Forms import SaveFileDialog

# GETS THE FILE PATH FROM THE USER THROUGH A FILE DIALOG

SaveFile = SaveFileDialog()

SaveFile.Filter = "Spotfire Text Data Format (*.stdf)|*.stdf"

SaveFile.ShowDialog()

if SaveFile.FileName=="":

    # user does not select a file (cancel button)

    saveFilename = ""

else:

    saveFilename = SaveFile.FileName

    print "saveFilename=", saveFilename

    #Export Table data to the file

    stream = FileStream(saveFilename, FileMode.Create)

    vizTable.As[TablePlot]().ExportData(DataWriterTypeIdentifiers.StdfDataWriter, stream)

    print "stream.Length = ", stream.Length

    # stream.Flush

    # stream.Close

    stream.Dispose

(10) Answers

Login