Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python equivalent of java OutputStream?

Is there a Python equivalent / pseudo-equivalent to java's OutputStream or PrintWriter?

I want to be able to have a handle that represents either a stream like stdout/sterr, or a file, or something else (a pipe or a socket or a dummy sink) and abstract away what kind of thing it is, so I can just send output to it.

How can I do this?

like image 514
Jason S Avatar asked Dec 17 '22 18:12

Jason S


1 Answers

"Abstracting away what type it is" happens automatically in Python - it's called 'duck typing'. Just pass any file-like object to the function, and have it use the interface of file-like objects.

FWIW, the standard input/output/error streams are represented by stdin, stdout and stderr in the sys module. To get file-like objects that read and write strings, use the StringIO module.

like image 180
Karl Knechtel Avatar answered Dec 29 '22 19:12

Karl Knechtel