Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f.write vs print >> f

Tags:

python

There are at least two ways to write to a file in python:

f = open(file, 'w')
f.write(string)

or

f = open(file, 'w')
print >> f, string     # in python 2
print(string, file=f)  # in python 3

Is there a difference between the two? Or is any one more Pythonic? I'm trying to write a bunch of HTML to file so I need a bunch of write/print statements through my file(but I don't need a templating engine).

like image 580
oxuser Avatar asked Dec 22 '11 00:12

oxuser


People also ask

What's different between print () and f write ()?

The print() function takes the supplied string argument, appends a newline character to the end, and calls the stdout. write() method to write it to standard output.

What is the difference between print and write?

The "Write" statement will put "" around the data you are outputting and the "Print" statement will not. The "Print" statement will also output data to the screen as well as an open file, where the "Write" statement only outputs to an open file.

What is print F in Python?

Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

What does F in print mean?

The fprintf function allows you to "write" information to the screen for the user to view. This very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.


3 Answers

print does things file.write doesn't, allowing you to skip string formatting for some basic things.

It inserts spaces between arguments and appends the line terminator.

print "a", "b" # prints something like "a b\n"

It calls the __str__ or __repr__ special methods of an object to convert it to a string.

print 1 # prints something like "1\n"

You would have to manually do these things if you used file.write instead of print.

like image 152
agf Avatar answered Oct 12 '22 21:10

agf


I disagree somewhat with several of the opinions expressed here, that print >> f is redundant and should be avoided in favour of f.write.

print and file.write are quite different operations. file.write just directly writes a string to a file. print is more like "render values to stdout as text". Naturally, the result of rendering a string as text is just the string, so print >> f, my_string and f.write(my_string) are nearly interchangeable (except for the addition of a newline). But your choice between file.write and print should normally be based on what you're doing; are you writing a string to a file, or are you rendering values to a file?

Sure, print is not strictly necessary, in that you can implement it with file.write. But then file.write is not strictly necessary, because you can implement it with the operations in os for dealing with file descriptors. Really they're operations on different levels, and you should use whichever is more most appropriate for your use (normally the level other nearby code is working on, or the highest level that doesn't get in your way).

I do feel that the print >> f syntax is fairly horrible, and is a really good example of why print should have been a function all along. This is much improved in Python 3. But even if you're writing Python 2 code that you're planning to port to Python 3, it is much easier to convert print >> f, thing1, thing2, thing3, ... to print(thing1, thing2, thing3, file=f) than it is to convert the circumlocution where you roll your own code to do the equivalent of print's rendering and then call f.write(text). I'm pretty sure the semi-automatic converter from Python 2 to Python 3 will even do the conversion for you, which it couldn't possibly do if you avoid the print >> f form.

Bottom line: use print to render values to stdout (or to a file). Use f.write to write text to a file.

like image 21
Ben Avatar answered Oct 12 '22 22:10

Ben


Agree with @agf

I preferred print(..., file=f) because of its flexibility.

with open('test.txt', 'w') as f:
    print('str', file=f)
    print(1, 20, file=f)

It is also easy to convert existing print command.

write accepts only string.

like image 4
wannik Avatar answered Oct 12 '22 20:10

wannik