[Tutor] open files

Sean 'Shaleh' Perry shaleh@valinux.com
Wed, 06 Sep 2000 14:25:19 -0700 (PDT)


On 06-Sep-2000 carol engle wrote:
> how do i open files. I read a book about Python and i wright programs but
> i cannot open them what is wrong somebody please tell me.

fp = open('/path/to/file', 'r')
for line in fp.readlines():
  print line, # comma is to suppress the new line character
close(fp)

A robust version would catch the exception open raises when it errors out.