[Tutor] Opening files?

Jeff Lohman jlohman@cox.net
Wed Mar 26 12:55:11 2003


Ok...I wrote and ran this:

>>> myTest = open("C:test.txt", 'r')
>>> for eachLine in myTest:
... 	print eachLine
...
>>> myTest.close()
>>>

No syntax errors...yay! But this was all I got:

>>> 


A carriage return and a prompt... how do I get the text in my file "test.txt" to print?

Futhermore, if I do the following:

>>> myTest = open("C:test.txt", 'r')
>>> print myTest

I get this:

<open file 'C:test.txt', mode 'r' at 0x0090CE18>
>>> 

What is the last bit? An address of sorts?

-Jeff


> 
> Your questions open up a whole interesting subject. Here's an 
> example of
> opening a .txt file using 2.2.2 on a Windows machine.
> 
> >>> myLimerick = open("E:/temp/lousyLimerick.txt", 'r')
> 
> The above line specifies the file to open, and the 'r' says 
> to open it only
> for reading. The next two lines tell the interpreter to print 
> each line of
> the file for viewing.
> 
> >>> for eachLine in myLimerick:
> ... 	print eachLine
> ...
> There was a programmer in Python
> 
> whose code (not his rhymes) were all right-on.
> 
> This limerick blows,
> 
> Who wrote it? Who knows?
> 
> For my eyeballs, this limerick's blight on.
> >>> myLimerick.close()
> >>>
> 
> When finished with the file, it is a good idea to explicitly 
>