User Tools

Site Tools


cs_lang:python:things-to-know:files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cs_lang:python:things-to-know:files [2010/12/12 18:53] – external edit 127.0.0.1cs_lang:python:things-to-know:files [2010/12/12 19:21] (current) cedric
Line 23: Line 23:
  
 <code python> <code python>
 +>>> a_file = None
 >>> try:               >>> try:              
 ...   a_file = open("./dico.txt", "r") ...   a_file = open("./dico.txt", "r")
 +...   text = a_file.read()
 ... except: ... except:
-...   pass+...   print("Error"
 +... finally: 
 +...   if a_file != None: 
 +...     a_file.close()
 ...  ... 
 >>>  >>> 
Line 36: Line 41:
  
 ===== 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>
 >>> with open("./dico.txt", "r") as f: >>> with open("./dico.txt", "r") as f:
-...   = f.read()+...   text = f.read()
 >>> >>>
->>> len(a)+>>> len(text)
 2664170 2664170
->>> print(a)+>>> print(text)
 . .
 . .
cs_lang/python/things-to-know/files.1292176385.txt.gz · Last modified: 2010/12/12 19:16 (external edit)