简体   繁体   中英

Send file contents in place of file name - Python 2

I am using a module with function foo(bar) where bar is supposed to be a str that contains the path for a text file that is read-in within foo .

Is it possible to create my_str = str(...) that contains the text that would otherwise be in bar and somehow pass my_str to foo in place of the text file.

Right off the bat, I would assume that this is not possible. It seems that if bar is used within open(bar, 'r') , then passing content to a function that is asking for a path does not make sense. Nonetheless I wanted to check with the stack's collective intelligence. Thanks.

You could populate a NamedTemporaryFile with the contents of your string and then pass in its name attribute. If you set it up with delete=True it'll auto-delete itself when it's closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM