Reputation: 77
palavra = raw_input('palavra: ')
arquivo = open('palavras.txt', 'r+')
lendo = arquivo.readline()
print palavra + lendo
arquivo.close()
I want to concatenate each line of the "palavras.txt" with the value of the variable "palavra" but in the code above it's only concatenating with one line and the rest is read but isn't concatenated.
Upvotes: 2
Views: 2219
Reputation: 19989
The problem is that you don't iterate through the other lines
with open('palavras.txt', 'r+') as f:
for lendo in f:
print palavra + lendo,
Upvotes: 3
Reputation: 59426
with open('palavras.txt') as palavrasFile:
print palavras.join(palavrasFile)
Use
print palavras + palavras.join(palavrasFile)
if you want the prepend the value of palavras
also to the first line. Spec isn't too clear on this.
Upvotes: 1
Reputation: 11070
read the whole of the file first
for line in arquivo.readlines():
palavra = palavra+line
print palavra
Upvotes: 1