AnsweredAssumed Answered

Running a script in the Spyder  console

Question asked by jborgion Champion on Feb 12, 2019
Latest reply on Feb 12, 2019 by lpinner

One of the things I like about the Spyder ide is the ability to highlight a block of code, and use 'Run Cell': I'll write several blocks of code and test them this way. As much of my work involves editing database field values, I'll often write original values to a text file before running an update cursor so I can see if what I'm trying to do actually works. Typically I'll do it like this:

 

blah blah blah...
logFile = r'C:\scripts\logs\name.txt'
logoutput = open(logFile,'w')
sys.stdout = logoutput # lets me use print() statements

blah
blah
print('Something')
blah
blah
print('Something else')

logoutput.close()


 

It's line 13 that goofs on me in Spyder.  If I .close() my file handle, the console window never returns to the In [#]: prompt, so it appears that the that the cell is still running.  If I open my text file it has all the records in it.  If I comment out line 13, the cell runs, and returns to the In [#]: prompt.  The text file is exactly the same as if I leave it as is.

 

There doesn't seem to be anything different syntax wise in open(), write() close() between Python 3.x or 2.x.  Anyone else seeing things like this?

Outcomes