How to print a file to stdout?

Python

Python Problem Overview


I've searched and I can only find questions about the other way around: writing stdin to a file.

Is there a quick and easy way to dump the contents of a file to stdout?

Python Solutions


Solution 1 - Python

Sure. Assuming you have a string with the file's name called fname, the following does the trick.

with open(fname, 'r') as fin:
    print(fin.read())

Solution 2 - Python

If it's a large file and you don't want to consume a ton of memory as might happen with Ben's solution, the extra code in

>>> import shutil
>>> import sys
>>> with open("test.txt", "r") as f:
...    shutil.copyfileobj(f, sys.stdout)

also works.

Solution 3 - Python

f = open('file.txt', 'r')
print f.read()
f.close()

From http://docs.python.org/tutorial/inputoutput.html

> To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").

Solution 4 - Python

My shortened version in Python3

print(open('file.txt').read())

Solution 5 - Python

you can also try this

print ''.join(file('example.txt'))

Solution 6 - Python

If you are on jupyter notebook, you can simply use:

!cat /path/to/filename

Solution 7 - Python

You can try this.

txt = <file_path>
txt_opn = open(txt)
print txt_opn.read()

This will give you file output.

Solution 8 - Python

If you need to do this with the pathlib module, you can use pathlib.Path.open() to open the file and print the text from read():

from pathlib import Path

fpath = Path("somefile.txt")

with fpath.open() as f:
    print(f.read())

Or simply call pathlib.Path.read_text():

from pathlib import Path

fpath = Path("somefile.txt")

print(fpath.read_text())

Solution 9 - Python

To improve on @bgporter's answer, with Python-3 you will probably want to operate on bytes instead of needlessly converting things to utf-8:

>>> import shutil
>>> import sys
>>> with open("test.txt", "rb") as f:
...    shutil.copyfileobj(f, sys.stdout.buffer)

Solution 10 - Python

Operating on the file's line iterator (if you open in text mode -- the default) is simple and memory-efficient:

with open(path, mode="rt") as f:
    for line in f:
        print(line, end="")

Note the end="" because the lines will include their line-ending char(s).

This is almost exactly one of the examples in the docs linked at (other) Ben's answer: https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmeteoritepanamaView Question on Stackoverflow
Solution 1 - PythonDavid AlberView Answer on Stackoverflow
Solution 2 - PythonbgporterView Answer on Stackoverflow
Solution 3 - PythonBenView Answer on Stackoverflow
Solution 4 - PythonmlanzeroView Answer on Stackoverflow
Solution 5 - PythonNinja420View Answer on Stackoverflow
Solution 6 - PythonAli HassaineView Answer on Stackoverflow
Solution 7 - PythonShoaibView Answer on Stackoverflow
Solution 8 - PythonRoadRunnerView Answer on Stackoverflow
Solution 9 - PythonmriconView Answer on Stackoverflow
Solution 10 - PythonBen MosherView Answer on Stackoverflow