print >> How to type in a file instead of writing

Asked 2 years ago, Updated 2 years ago, 102 views

I write like the code below when I type in the file. I heard that writing >> is a bad way, so which one is a good way?

f = open("input.txt", "w+")
print >> f, "hello"

python file-io

2022-09-22 13:56

1 Answers

print >> is not supported by Python 3.

To enter a file with a print, see

from__future__import print_function #Required under Python 2.6
...
print("hello", file=f)

in the same way as

If you don't use print, f.write("hello\n") is present.


2022-09-22 13:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.