Phantom of Krankor
Phantom of Krankor

Reputation: 105

python eval(repr(file_handle)) fails with synax error

I'm trying to pass a file handle through some hoops that lead to me wanting to pass the file handle as it's repr to pull it back into existence later on. But, I've run into a problem. The built-in repr for file handles is broken!

$ python3
Python 3.9.4 (default, Apr  6 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("x.out","a")
>>> f_copy = eval(repr(f))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    <_io.TextIOWrapper name='x.out' mode='a' encoding='UTF-8'>
    ^
SyntaxError: invalid syntax
>>> 

I can't find anything that would imply that I'd have to make my own custom subclass of io.TextIOWrapper or anything like that. What am I missing?

Upvotes: 0

Views: 84

Answers (0)

Related Questions