Skip to main content
gamedev.net
🔒 Locked

Reading files with Python

Started by Kaize Jul 1, 2004 at 11:26 AM 8 replies 650+ views
Original Post
Kaize
Kaize
I'm pretty new to python. I'm having trouble getting python to read and display what a files says. Like a txt file. Anyone know what I have to type to get python to look for the file, read, and display what the file says? edit: I know how to make python read files, I just want it to display what it says.
Kaize
Kaize
That was fast. ;p
It displays the txt now, but its in double space for some reason. Know how to fix that?
Fruny
Fruny
The newline character at the end of each ... line, is included in the strings you are iterating on. And print itself does print a newline too. So you get two newlines.

Fix: Either remove the newline from the strings as you go:

myfile = open('bla.txt')for line in myfile:    print line[:-1]


Alternatively:

myfile = open('bla.txt')for line in myfile:    print line.rstrip("\r\n")


Or have print not add a newline

myfile = open('bla.txt')for line in myfile:    print line,



Note that you can use file instead of open.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Kaize
Kaize
Thanks!
civguy
civguy
Or if you don't care about iterating through the lines, just do

print file('bla.txt').read()
bytecoder
bytecoder
The Python Documentation is the definite resource if you want to know how to do something in Python, and google is the second.
EDIT:
And by the way, I think it's prefered to use list comprehension and such instead of for loops, e.g:
file = open(...)print "".join([line for line in file])

I also recommend Dive Into Python if you already know how to program; it's a great book.
civguy
civguy
Quote:
Original post by bytecoder
And by the way, I think it's prefered to use list comprehension and such instead of for loops
That applies mostly if you want to construct a list with a simple loop. In this case it seems that one just wants to print out stuff, and I don't see list comprehensions that suitable.
Fruny
Fruny
Quote:
Original post by bytecoder
And by the way, I think it's prefered to use list comprehension and such instead of for loops, e.g:
file = open(...)print "".join([line for line in file])



There is no point in constructing a list comprehension : a file is a lazy iterable object. As such, it is designed to allow efficient for loops.

if you want the file as a single string, just do f.read().
If you want a list of strings, just do f.readlines() or even list(f) (build a list from an iterable).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
daerid
daerid
God python has got to have one of the most readable and flexible syntaxes on the planet.
daerid@gmail.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.