Running a script in the Spyder console

3185
4
02-12-2019 11:51 AM
JoeBorgione
MVP Emeritus

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?

That should just about do it....
Tags (1)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

I have found with non-inline output, that you 'close' outside.  For example, if you set matplotlib graphs to anything other than 'inline' in your preferences, the graph opens up in a separate window.  you close it from there.

There should be no need to close the file, since it should close itself, unless there is some reason to keep it open? but the [in] prompt would appear if it was still open.

If it bugs you check

GitHub - spyder-ide/spyder: Official repository for Spyder - The Scientific Python Development Envir... 

ccordoba12 is one of the chief maintainers and he would probably have a specific explanation because of the interlinkages of spyder, pyqt and all the other dependencies

0 Kudos
JoeBorgione
MVP Emeritus

Just something I learned early on: open it, write to it, close it.  I'm okay with leaving it...

That should just about do it....
0 Kudos
Luke_Pinner
MVP Regular Contributor

I don't use spyder, but does using a context manager have the same issue?  Actually, I bet it probably does.

I think you should save and then reset sys.stdout after you rediredirect it to a file.. I would guess that you lose your prompt, because by closing the file, you've closed sys.stdout...

import sys

logFile = r'C:\temp\test.txt'
stdout = sys.stdout  #<== save original sys.stdout
with open(logFile, 'w') as logoutput:
    sys.stdout = logoutput  # lets me use print() statements
    print('Blah')

sys.stdout = stdout  #<== reset sys.stdout

print(logoutput.closed)  # Check it's closed
print(open(logFile).read())  # Check what was written to it earlier
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Output:

True
Blah
‍‍
Luke_Pinner
MVP Regular Contributor

And to avoid messing with sys.stdout at all:

Python 2

from __future__ import print_function
with open('filename', 'w') as f:
    print('some text', file=f)

Python 3

with open('filename', 'w') as f:
    print('some text', file=f)