让字符串充当文件
Let string act as file
我正在与一个图书馆合作,该图书馆希望我以文件名的形式向其传递数据。然后它将打开该文件并读取数据。我有一个字符串中的数据,我不想将它写入文件(因为我不想事后删除它)。
有没有一种方法可以将字符串转换为流并生成一个文件名,使我的库可以打开我的流并访问我的字符串的内容?
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
我正在与一个图书馆合作,该图书馆希望我以文件名的形式向其传递数据。然后它将打开该文件并读取数据。我有一个字符串中的数据,我不想将它写入文件(因为我不想事后删除它)。
有没有一种方法可以将字符串转换为流并生成一个文件名,使我的库可以打开我的流并访问我的字符串的内容?
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