Created
April 28, 2015 09:39
-
-
Save bewt85/3c3913655bd051e2eb6e to your computer and use it in GitHub Desktop.
Python dodgy file handle modes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('foo', 'w') as foo: | |
foo.write("bar\n") # writes bar | |
with open('foo', 'wa') as foo: | |
foo.write("baz\n") # writes baz, removes bar | |
with open('foo', 'what will this do?') as foo: | |
foo.write("nonsense\n") # removes bar, writes nonsense! | |
with open('foo', 'something else') as foo: | |
foo.write("This fails\n") # raises `ValueError: mode string must begin with one of 'r', 'w', 'a' or 'U', not 'something else'` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment