1

I am trying to write the following loop to a .txt file (in addition to printing the output to terminal). On doing so I get an error that says:

> TypeError:function takes exactly 1 argument (2 given)

I understand what the error is trying to convey, but is there a way to work around this. I do need the labels and the results output to the .txt file just like they are printed on the terminal

Here is the code I am using:

out_write = open(write_to, 'wb')

for arr in top_k:
    print(labels[arr], results[arr])
    out_write.write(labels[arr], results[arr])
print ("\n\n\n")
out_write("\n\n\n")
out_write.close()

I am working in python 2.7

2
  • 1
    out_write.write('{} {}'.format(labels[arr], results[arr])) Commented Nov 22, 2017 at 2:36
  • 2
    Make a single string (however you like to do that: concatenation, %, .format(), f-strings) or use two calls to .write() Commented Nov 22, 2017 at 2:39

1 Answer 1

6

Python offers many solutions to this problem. You can combine your arguments to make a single string using several methods:

  • Convert to string and concatenate
    out_write.write(str(labels[arr]) + " " + str(results[arr]) + "\n")
  • Old-school string interpolation using %
    out_write.write("%s %s\n" % (labels[arr], results[arr]))
  • str.format()
    out_write.write("{} {}\n".format(labels[arr], results[arr]))
  • f-strings, the new hotness
    out_write.write(f"{labels[arr]} {results[arr]}\n")

Or you can use print() itself:

print(labels[arr], results[arr], file=out_write)

To avoid the need to specify the file parameter in every call to print(), you can use functools.partial:

file_out = functools.partial(print, file=out_write)
file_out(labels[arr], results[arr])

Or a lambda!

file_out = lambda *args: print(*args, file=out_write)
file_out(labels[arr], results[arr])

Why not write a function that prints and writes at the same time? One call does it all!

out = lambda *args: print(*args) and print(*args, file=out_write)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I took your str.format() advice, but it outputs the text onto the same line. Im sure I'm missing something very basic to go into a new line after it prints a "label result" combo. Thanks!
Sorry, forgot to put newlines into the format strings. I just edited my answer to add those..
Perfect! That did it. Just one more thing, I am running this loop over multiple files, so essentially this for loop gets run multiple times but I think my code only prints the outputs to the for loop once (on the text file), the terminal has as many for loops as there are files in my dir since I am looping over them. What would a reason for this not being reflected in the out.txt? Thanks :)
I'd ask a new question for that.
Figured it out, its essentially just out_write = open(write_to, 'a') instead of the 'wb'!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.