Verified Commit e7161685 authored by Renato Alves's avatar Renato Alves 🌱

Mention mode of safe_write()

parent 8dd1ae92
......@@ -1416,13 +1416,11 @@ OMG! I have received A MESSAGE!!!
> However, consider the situation where `y` is `0` and a `ZeroDivisionError` exception happens.
> If unhandled, the `with` will ensure the file is `closed()` but you will be left with a half-written (or corrupted) file.
>
> To avoid this situation your task is to create a better version of `open()` that we will call `safe_write()`.
> `safe_write()` should do the same as `open()`,
> To avoid this situation your task is to create a better version of `open()` that we will call `safe_write()`.
> `safe_write()` should do the same as `open()` in `wt` mode,
> but in addition should delete the file if an error occurs.
>
> To make your life easier, consider using the versatile `contextmanager` from [`contextlib` library][contextlib].
> This library provides an easy way to transform a generator into a context manager,
> that is, something that can be used together with the `with` keyword.
>
> With your solution, the following code should raise a `ZeroDivisionError` and `I_should_be_deleted.csv` should not remain once the script finishes. (The standard library module `os` includes a `remove` function that can help you with this part.)
> ~~~
......@@ -1449,7 +1447,7 @@ OMG! I have received A MESSAGE!!!
> >
> > @contextmanager
> > def safe_write(filename):
> > with open(filename, 'w') as fh:
> > with open(filename, 'wt') as fh:
> > try:
> > yield fh
> > except ZeroDivisionError:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment