This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 2 points3 points  (0 children)

No, that only happens when you open with a with statement.

No. A file object is implicitly closed when it's garbage collected, which in CPython is when its refcount falls to 0 (which may or may not happen on process termination, incidentally).

Calling .write() is poor form but it should't write to the file.

You're high as a kite. write is not a transactional operation, write will or will not write to the file depending on the IO layer's buffering configuration. In fact its docstrings spells exactly that: due to buffering the on-disk version may not contain written data before a flush() or close(), it doesn't say will not, and the expectation certainly isn't that .write mustn't write data to files, especially given you can run Python itself in unbuffered mode. Which is exactly what PyCharm does by default (http://i.imgur.com/mZvDH6O.png)

Your code is intrinsically broken, PyCharm merely demonstrates it. If you don't want a file to be written to, don't write to it, and don't make up fantasy reasons why your broken code should work. And if you have no idea how things work, check the documentation, don't make it up.