if data.find('!masters') != -1:
f = open('masters.txt')
lines = f.readline()
for line in lines:
print lines
sck.send('PRIVMSG ' + chan + " " + str(lines) + '\r\n')
f.close()
masters.txt has a list of nicknames, how can I print every line from the file at once?. The code I have only prints the first nickname. Your help will be appreciate it. Thanks.
You could try this. It doesn't read all of f into memory at once (using the file object's iterator) and it closes the file when the code leaves the with block.
If you're using an older version of python (pre 2.6) you'll have to have
Firstly, as @l33tnerd said,
f.close
should be outside the for loop.Secondly, you are only calling
readline
once, before the loop. That only reads the first line. The trick is that in Python, files act as iterators, so you can iterate over the file without having to call any methods on it, and that will give you one line per iteration:Finally, you were referring to the variable
lines
inside the loop; I assume you meant to refer toline
.Edit: Oh and you need to indent the contents of the
if
statement.You probably want something like:
Don't close it every iteration of the loop and print line instead of lines. Also use readlines to get all the lines.
EDIT removed my other answer - the other one in this discussion is a better alternative than what I had, so there's no reason to copy it.
Also stripped off the \n with read().splitlines()
Loop through the file.
Did you try
?
only reads "a line", on the other hand
reads whole lines and gives you a list of all lines.