Skip to content

Instantly share code, notes, and snippets.

@89465127
Created April 26, 2013 18:58
Show Gist options
  • Save 89465127/5469559 to your computer and use it in GitHub Desktop.
Save 89465127/5469559 to your computer and use it in GitHub Desktop.
How to mock a file in memory for testing and experimenting.
import StringIO
# Create mock file
mock_file = StringIO.StringIO()
for i in range(20):
mock_file.write("{}\n".format(i))
mock_file.seek(0)
# Do something
for line in mock_file:
print line,
# Cleanup
mock_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment