I'm writing a gui for ironpython and I'm trying create a text file that the user can name in a textbox. The textfile is being used to give me a python script from a vector file. Any suggestions
Asked
Active
Viewed 1,212 times
0
-
Welcome on StackOverflow ! What have you tried so far ? – Fabich Jul 22 '16 at 10:18
-
Thank you. I have tried using SafeFileDialog() and Spotfire(http://stackoverflow.com/questions/32243732/output-spotfire-print-to-text-file). – Cian Jul 22 '16 at 11:19
2 Answers
0
With the following I was able to create a file with IronPython
ScriptEngine m_engine = Python.CreateEngine();
ScriptScope m_scope = m_engine.CreateScope();
String code = @"file = open('myfile.txt', 'w+')";
ScriptSource source = m_engine.CreateScriptSourceFromString(code, SourceCodeKind.SingleStatement);
source.Execute(m_scope);
Here's the Python documentation about the w+
. It opens the file for writing, truncating the file first.
So I guess you just need to insert the name of the file in the script...

Timothée Bourguignon
- 2,190
- 3
- 23
- 39
0
I am putting up the code that worked for me when I was writing in python. Hope it helps.
def on_save_as(self, sender, e):
dlg = SaveFileDialog()
dlg.Filter = "Text File (*.txt)|*.txt"
dlg.Title = "SaveFile"
if dlg.ShowDialog():
self.save_file(dlg.FileName)
def save_file(self, filename):
sw = StreamWriter(filename)
try:
buf = self.textbox.Text
sw.Write(buf)
self.statusbar_item.Content = Path.GetFileName(filename)
self.openfile = filename
except Exception, ex:
MessageBox.Show(ex.Message)
finally:
sw.Close()

Cian
- 3
- 2