Python Forum
[newbie] question on what to put in () on target.write
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[newbie] question on what to put in () on target.write
#1
#I know how to put in line1 variable on line 25 (target.write(line1)), however when I try to put line2 in it I get an error. The error is "function takes exactly 1 argument (2 give).

How do you do 2 or 3 variables in this?

Thanks,

Matt

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C(^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

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

print "And finally, we close it."
target.close()
Reply
#2
You could try:

target.write(line+line2)
Reply
#3
The write() method takes only one argument. So, you can concatenate the two variables as was proposed or write them one after another.
target.write(line1)
target.write(line2)
But entering three inputs is a lot of typing. You can do:

for _ in range(3):
    line = raw_input( 'Line : ')
    target.write(line)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Sep-14-2017, 06:29 AM)Sagar Wrote: You could try:

target.write(line+line2)

The way the code prints now (from yours) is:
"line1line2"

How would I write the code so that it printed stacked on top of each other?

So that it looked like:

"line 1
Line 2
line 3"

I know you can use the escape factor \n but I'm not sure how. The task in my book I'm working through is to use 1 target.write() with format characters, escapes and strings to have it print:

"line1
line2
line3"
Reply
#5
Yes you are correct, new line character is needed to get the output as desired.
target.write(line1 + "\n" + line2 + "\n" + line3)
Reply
#6
Or for output on one line with spacing:
[python]target.write (line1 + " " + line2 + " " + line3)[python]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 691 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 676 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 594 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Pyautogui, İmagesearch, Moving target Beyazx 0 585 Jun-27-2023, 08:47 PM
Last Post: Beyazx
  Newbie.... run for cover. OpenCV question Stevolution2023 2 968 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  What is the Target value in this GridSearchCV problem? Led_Zeppelin 0 657 Feb-15-2023, 07:32 PM
Last Post: Led_Zeppelin
  numpy newbie question bcwilly_ca 4 1,177 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  How to read python shortcut target profile directory of Chrome Ink file sunny9495 1 1,656 Apr-12-2022, 06:12 PM
Last Post: sunny9495
  Question from complete python's newbie Davicom 3 2,364 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Control Mouse and Keyboard Across the Country Without VNC on Target PC Khuber79 5 2,984 Feb-21-2021, 02:42 AM
Last Post: NullAdmin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020