Print to file in Python: redirecting against print file argument vs write

I have a bunch of print calls that I need to write to a file instead of stdout . (I don't need stdout at all).

I consider three approaches. Are there any benefits (including performance) for any of them?

The full redirect that I saw here :

 import sys saveout = sys.stdout fsock = open('out.log', 'w') sys.stdout = fsock print(x) # and many more print calls # later if I ever need it: # sys.stdout = saveout # fsock.close() 

Redirection in each print statement:

 fsock = open('out.log', 'w') print(x, file = fsock) # and many more print calls 

Recording function:

 fsock = open('out.log', 'w') fsock.write(str(x)) # and many more write calls 
+6
redirect python file stdout
source share
3 answers

I would not expect any lasting performance differences between these approaches.

The advantage of the first approach is that any reasonably correct code you rely on (the modules you import) will automatically pick up your desired redirection.

The second approach has no advantage. It is only suitable for debugging or discarding code ... and not even a good idea for that. You want your exit decisions to be grouped into several clearly defined places, rather than scattered across your code every time you call print() . In Python3, print() has a function, not an operator. This allows you to override it if you want. So you can def print(*args) if you want. You can also call __builtins__.print() if you need access to it, for example, in the definition of your own print() .

The third approach ... and, as a rule, the principle that all of your output should be generated in certain functions and class methods that you define for this purpose ... is probably best.

You should separate your output and formatting from the main functionality as much as possible. Keeping them separate allows you to use the kernel. (For example, you can start with something that was intended to be run from the text / shell console, and later you need to provide a web interface, a full-screen (damned) interface or a graphical interface for it. You can also create completely different functions around it. .. in situations where the resulting data should be returned in its native form (as objects), and not be inserted as text (output) and re-processed into new objects.

For example, I had several cases when I wrote several complex queries and collected data from different sources and printed a report ... say about inconsistencies ... later you need to adapt to a form that could spit out data in some form (for example, YAML / JSON), which can be transferred to some other system (for example, to coordinate one data source with another.

If from the very beginning you save the basic operations separately from the output and formatting, then such an adaptation is relatively simple. Otherwise, this entails quite a lot of refactoring (sometimes equivalent to a complete rewrite).

+6
source share

From the file names you use in your question, it looks like you want to create a log file. Have you looked at the Python logging module instead?

+4
source share

I think semantics are important:

I propose the first approach for a situation when you print the same material that you will print on the console. The semantics will be the same. For a more complicated situation, I would use a standard registration module.

The second and third approaches are slightly different if you are printing text strings. The second approach - print adds a new line, but write does not.

I would use the third approach mainly in binary or non-text format, and I would use redirection in the print statement in most other cases.

+4
source share

All Articles