NEED HELP- read file contents, while loop to accept user input, and enter to exit

Tim Golden mail at timgolden.me.uk
Tue May 24 04:46:11 EDT 2011


On 24/05/2011 09:31, Cathy James wrote:
> dear mentor,
> I need help with my code:
> 1) my program won't display file contents upon opening

> #1) open file and display current file contents:
> f = open ('c:/testing.txt'', 'r')
> f.readlines()

If you're running this in an interactive interpreter, I would
expect it to show a list of lines (assuming c:/testing.txt has
something in it...). If you're running it as a program, though,
it won't show anything: you need to actually output the result
of the expression f.readlines (). It only happens at the
interpreter as a development convenience:

f = open ("c:/testing.txt", "r")
print f.readlines ()
# or print (f.readlines ()) if you're in Python 3

TJG



More information about the Python-list mailing list