0

Hii need help for writing user input in a file. The following code is writing only the last word in a file. For example if user type "hello world", it will save only "world" in the file. I want to write the whole sentence in the file.

for i in range(0,2):
    orange = open('original.txt', 'w')
    text=raw_input("Enter message:")
    orange.writelines(text)
    orange.close()

Edited by techyworld

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by pyTony
0

Try something like this ...

for i in range(0,2):
    orange = open('original.txt', 'a')
    text = raw_input("Enter message:")
    orange.write(text + '\n')

orange.close()
0

Alternatively:

line_count = 3
with open('original.txt', 'a') as orange:
    orange.writelines(('%s\n' % raw_input("Enter message %i:" % ind))
                        for ind in range(1, 1+line_count))
0

One more.

with open('original.txt', 'a') as f_out:
    for i in range(0,2):
        text = raw_input("Enter message:")
        f_out.write('{}\n'.format(text))
0

I want to write "helloworld" lke this in the file, means it concatenate it. I dont want it to be on different lines

0

Then just replace 4th line of snipsat with f_out.write(text)

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.