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()

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()

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))

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))

what does 'a' means?

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.