Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: printing a file to stdout

Tags:

python

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?

like image 502
meteoritepanama Avatar asked Nov 10 '11 18:11

meteoritepanama


People also ask

Does Python print go to stdout?

In Python, whenever we use print() the text is written to Python's sys. stdout, whenever input() is used, it comes from sys. stdin, and whenever exceptions occur it is written to sys. stderr.

How do you assign a print output to a variable in Python?

So print() also a function with the return value with None . So the return value of python function is None . But you can call the function(with parenthesis ()) and save the return value in this way. So the var variable has the return value of some_function() or the default value None .


1 Answers

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()) 
like image 99
David Alber Avatar answered Oct 18 '22 16:10

David Alber