cs_lang:python:things-to-know:files
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
cs_lang:python:things-to-know:files [2010/12/12 18:53] – external edit 127.0.0.1 | cs_lang:python:things-to-know:files [2010/12/12 19:20] – cedric | ||
---|---|---|---|
Line 23: | Line 23: | ||
<code python> | <code python> | ||
+ | >>> | ||
>>> | >>> | ||
... | ... | ||
+ | ... text = a_file.read() | ||
... except: | ... except: | ||
- | ... pass | + | ... print(" |
+ | ... finally: | ||
+ | ... if a_file != None: | ||
+ | ... | ||
... | ... | ||
>>> | >>> | ||
Line 37: | Line 42: | ||
===== The with statement ===== | ===== The with statement ===== | ||
The better solution. No need to use **try: ... except: ...**. | The better solution. No need to use **try: ... except: ...**. | ||
+ | Forget the previous method. | ||
<code python> | <code python> | ||
>>> | >>> | ||
- | ... a = f.read() | + | ... text = f.read() |
>>> | >>> | ||
- | >>> | + | >>> |
2664170 | 2664170 | ||
- | >>> | + | >>> |
. | . | ||
. | . |
cs_lang/python/things-to-know/files.txt · Last modified: 2010/12/12 19:21 by cedric