让字符串充当fi

2024-04-26 00:53:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个库,它希望我以文件名的形式传递数据。然后它将打开该文件并读取数据。我有一个字符串中的数据,我不想把它写到一个文件中(因为我不想在以后删除它)。你知道吗

有没有一种方法可以将字符串转换为流并生成一个文件名,以便我的库打开我的流并访问字符串的内容?你知道吗


Tags: 文件数据方法字符串内容文件名读取数据形式
1条回答
网友
1楼 · 发布于 2024-04-26 00:53:00
import tempfile

fh = tempfile.NamedTemporaryFile()  # this creates an actual file in the temp directory
fh.write(my_string)  
print fh.name
call_other_thing(fh.name)
fh.close()  # file is now deleted

相关问题 更多 >