7/9 With and As Keywords


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:

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?


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.

Context Managers
In order to use the with statement, you must import from future.

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


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