<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Lie Ryan wrote:
<blockquote cite="mid:hme4dk$66g$1@dough.gmane.org" type="cite">
  <pre wrap="">On 03/01/10 02:49, Karim Liateni wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Lie Ryan wrote:
    </pre>
    <blockquote type="cite">
      <pre wrap="">On 03/01/10 01:12, Alan Gauld wrote:
  
      </pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre wrap="">def getLines(file):
  """Get the content of a file in a lines list form."""
  f = open(file, 'r')
  lines = f.readlines()
  f.close()
  return lines
      
          </pre>
        </blockquote>
        <pre wrap="">I'm not sure these functions add enough value to ghave them. I';d
probably just use

try: open(outfile,'w').writelines(lines)
except IOError: #handle error

try: lines = open(filename).readlines()
except IOError: #handle error

The close will be done automatically since you don't hold a reference to
the file
    
        </pre>
      </blockquote>
      <pre wrap="">Remember why we have the new with-block? It's precisely because files
will be automagically closed only if we're running on CPython; Python
the language and thus alternative implementations doesn't guarantee
automatic closing. I'd agree with the function having minimal utility
value though:

with open(file) as f:
    lines = f.readlines()
    # f.close() will be called by the context manager

and if you're just copying to another file:

from contextlib import nested
with nested(open(infile), open(outfile, 'w')) as (fin, fout):
    fout.write(fin.read())

or even better, as Alan suggested, using shutil.copyfile().

_______________________________________________
Tutor maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
To unsubscribe or change subscription options:
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>

  
      </pre>
    </blockquote>
    <pre wrap="">Thank you Lie but I have a restriction on the python version I must use
v2.2.
This feature is available only on later version 2.5 I believe.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Then you should at the least use the try-finally block, I think that one
has been there since 2.2? If you didn't use try-finally, there is no
guarantee that f.close() would be called if an exception happened in the
middle of reading/writing (e.g. KeyboardInterrupt, or perhaps user
plugging off the thumbdrive, or bit more realistic having a full
harddisk or exceeded some disk quota)

_______________________________________________
Tutor maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
To unsubscribe or change subscription options:
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>

  </pre>
</blockquote>
Thank you Lie.<br>
Yes, in fact I did it all the time during my years of Java development.<br>
In perl I used or open() or die( "msg" ) structure.<br>
I bypassed it because I wanted to construct the blocs very quickly like
a 'beginner'.<br>
I was excited to make it work as fast as ossible to see if I can
program something<br>
decent in Python. (gaining confidence? haaa human nature!).<br>
<br>
I definitely enjoy python (and not very far in spirit my loving Lisp).<br>
I definitely HATE tcl, java.<br>
<br>
Now I will improve quality and robustness.<br>
<br>
Thanks a lot!<br>
<br>
Karim<br>
<br>
</body>
</html>