Skip to content

Instantly share code, notes, and snippets.

@bewt85
Created April 28, 2015 09:39
Show Gist options
  • Save bewt85/3c3913655bd051e2eb6e to your computer and use it in GitHub Desktop.
Save bewt85/3c3913655bd051e2eb6e to your computer and use it in GitHub Desktop.
Python dodgy file handle modes
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