I want to write a Python function that iterates over lines of the input argument, but can accept either a string or a file-like object. Is there a way to do this?
def myfunc(x):
for line in x:
doSomething(line)
The above code works for a file-like object but not strings, and I’d rather write just one function rather than two, since my real task is a lot more complex than the above.
There are about a million variants you could use here with
try-exceptAnother version (slightly less permissive — see comments):