25

I have the following code:

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

Here target is the file object and line1, line2, line3 are the user inputs. I want to use only a single target.write() command to write this script. I have tried using the following:

target.write("%s \n %s \n %s \n") % (line1, line2, line3)

But doesn't that put a string inside another string but if I use the following:

target.write(%s "\n" %s "\n" %s "\n") % (line1, line2, line3)

The Python interpreter(I'm using Microsoft Powershell) says invalid syntax. How would I able to do it?

kartikeykant18
  • 1,737
  • 6
  • 28
  • 42

9 Answers9

48

You're confusing the braces. Do it like this:

target.write("%s \n %s \n %s \n" % (line1, line2, line3))

Or even better, use writelines:

target.writelines([line1, line2, line3])
aIKid
  • 26,968
  • 4
  • 39
  • 65
  • ok using %(line1, line2, line3) outside target.write() makes them unavailable.. but where's the second " – kartikeykant18 Jan 09 '14 at 12:17
  • Can you tell me what do you mean by `using %(line1, line2, line3) outside target.write() makes them unavailable`? – aIKid Jan 09 '14 at 12:20
  • 2
    @kartikey_kant: `%` is an operation on the string, not the write call. The way you had it, you were telling Python to write a string with `%s` in it, then apply the `%` operator to the result of the `write` call. – user2357112 Jan 09 '14 at 12:22
  • It means I was writing target.write() %(line1, line2, line3) – kartikeykant18 Jan 09 '14 at 12:22
  • yeah @user2357112 that what i meant by using %(line1, line2, line3) outside target.write() makes them unavailable – kartikeykant18 Jan 09 '14 at 12:24
  • @kartikey_kant Ah i see what you meant by the first comment. Sorry, that was a typo! – aIKid Jan 09 '14 at 12:26
  • 2
    Works for me, but you need to remove the spaces in between the placeholders or your output will have leading spaces on the second and third new lines. Didn't know about writelines, thanks for mentioning. – bran.io Jun 28 '15 at 01:12
  • 5
    Note that writelines doesn't add line endings. – sage88 Jun 07 '17 at 05:38
  • Yes bran.io is right. If you want correct formatting you should instead write: target.write("%s\n%s\n%s\n" % (line1, line2, line3)) – LucCW Jun 20 '17 at 01:18
10

another way which, at least to me, seems more intuitive:

target.write('''line 1
line 2
line 3''')
jcklopp
  • 451
  • 5
  • 9
7
with open('target.txt','w') as out:
    line1 = raw_input("line 1: ")
    line2 = raw_input("line 2: ")
    line3 = raw_input("line 3: ")
    print("I'm going to write these to the file.")
    out.write('{}\n{}\n{}\n'.format(line1,line2,line3))
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
3

I notice that this is a study drill from the book "Learn Python The Hard Way". Though you've asked this question 3 years ago, I'm posting this for new users to say that don't ask in stackoverflow directly. At least read the documentation before asking.

And as far as the question is concerned, using writelines is the easiest way.

Use it like this:

target.writelines([line1, line2, line3])

And as alkid said, you messed with the brackets, just follow what he said.

2

It can be done like this as well:

target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
1

Assuming you don't want a space at each new line use:

print("I'm going to write these to the file")
target.write("%s\n%s\n%s\n" % (line1, line2, line3))

This works for version 3.6

Marco_hm
  • 11
  • 2
1

this also works:

target.write("{}" "\n" "{}" "\n" "{}" "\n".format(line1, line2, line3))
Machavity
  • 30,841
  • 27
  • 92
  • 100
jaydeemourg
  • 133
  • 1
  • 1
  • 8
0

You could use the join method of Python strings.

target.writelines("\n".join([line1, line2, line3]))
target.write("\n")

From https://docs.python.org/3/library/stdtypes.html#str.join :

Return a string which is the concatenation of the strings in iterable.

Nerveless_child
  • 1,366
  • 2
  • 15
  • 19
-2
variable=10
f=open("fileName.txt","w+") # file name and mode
for x in range(0,10):
    f.writelines('your text')
    f.writelines('if you want to add variable data'+str(variable))
    # to add data you only add String data so you want to type cast variable  
    f.writelines("\n")