How to do virtual file processing?

后端 未结 5 763
滥情空心
滥情空心 2020-11-27 14:15

So for creating files I use the following:

fileHandle = open(\'fileName\', \'w\')

then write the contents to the file, close the file. In t

5条回答
  •  醉话见心
    2020-11-27 14:51

    You can use StringIO as a virtual file , from official documentation

    from io import StringIO
    
    output = StringIO()
    output.write('First line.\n')
    print >>output, 'Second line.'
    
    # Retrieve file contents -- this will be
    # 'First line.\nSecond line.\n'
    contents = output.getvalue()
    
    # Close object and discard memory buffer --
    # .getvalue() will now raise an exception.
    output.close()
    

提交回复
热议问题