Created
April 26, 2013 18:58
-
-
Save 89465127/5469559 to your computer and use it in GitHub Desktop.
How to mock a file in memory for testing and experimenting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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