[Tutor] new to python
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Jul 23 04:03:00 EDT 2017
On 23/07/17 07:26, N6Ghost wrote:
>
> f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
> for line in file:
Note that you have no variable called 'file'.
So this line doesn't make sense.
> for line in f:
> print(line.rstripe())
This bit will work if you omit the line above and
fix the indentation. (and remove the 'e' from strip()
> f.close()
This should be outside the loop, you don't want
to close the file after every line.
Finally, there is another way to do this which
is considered 'better'/more Pythonic:
with open("C:\coderoot\python3\level1\inputfile.txt", 'r') as f:
for line in f:
print(line.strip())
Notice with this construct the closing of the file is
handled for you.
> any idea why that does not work?
When posting questions always include the full error text.
Although apparently cryptic it actually contains a lot of
useful detail which saves us from making guesses.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list