I have some lines in Python that I want to pass a function that for some reason only accepts inputs as filepaths. For the example, let's call this function func.read()
.
Since func.read()
only knows how to reads files, not strings directly, I'm being forced to write my text into an auxiliary file and then pass the file path to func.read()
:
text="""some
random
text"""
fout = open("aux","wt")
fout.write(text)
fout.close()
func.read("aux")
But this is a hassle, and I'd like to avoid relying on writing external files. I could modify the function to take a string or list of strings, but this is a last case scenario (for some other reasons I can't go into detail here).
Is there a way I can "trick" this function by creating an object that behaves like a path to a file? Essentially an object that can be passed to open()
I'd say.
Cheers