7/9 With and As Keywords


#1

Hi, I completed exercise 7/9 The ‘with’ and ‘as’ Keywords on Codecademy, but I have another issue. I am coding in Jython Environment for Students (JES) for a class and I wanted to practice the exercise there. When I type in the same code:

with open("text.txt", "w") as textfile:
    textfile.write("Success!")

JES tells me that there is a syntax error. It does not give me any more information beyond that.

Invalid syntax.
Your code contains at least one syntax error, meaning it is not legal Jython.

I’m a little confused why Codecademy likes it, but not JES. I understand that Jython is a variation of Python, so could that be why there is a problem?


#2

Files
listing 2-31
The JVM does note immediately garbage collect, so proper de-allocation of resources is more important. The close() method should be called on a file when we are through working with it. The proper methodology to use when working with a file is to open, process, and then close each time.
http://www.jython.org/jythonbook/en/1.0/DataTypes.html


Context Managers
http://www.jython.org/jythonbook/en/1.0/ExceptionHandlingDebug.html#context-managers
In order to use the with statement, you must import from future.

See the above chapter/section for more reading on with.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.