In [1]: import os
In [2]: f = file(os.devnull, "w")
In [3]: %timeit f.writelines( "%s\n" % item for item in xrange(2**20) )1 loops, best of 3: 385 ms per loop
In [4]: %timeit f.writelines( ["%s\n" % item for item in xrange(2**20)] )ERROR: Internal Python error in the inspect module.Below is the traceback from this internal error.
Traceback (most recent call last):...MemoryError
In [4]: %timeit f.writelines( "%s\n" % item for item in xrange(2**20) )1 loops, best of 3: 370 ms per loop
In [5]: %timeit f.writelines( ["%s\n" % item for item in xrange(2**20)] )1 loops, best of 3: 360 ms per loop
#!/usr/bin/python
# Open a filefo = open("foo.txt", "rw+")seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.line = fo.writelines( seq )
# Close opend filefo.close()
poem = '''\Programming is funWhen the work is doneif you wanna make your work also fun:use Python!'''f = open('poem.txt', 'w') # open for 'w'ritingf.write(poem) # write text to filef.close() # close the file
import jsona = [1,2,3]with open('test.txt', 'w') as f:f.write(json.dumps(a))
#Now read the file back into a Python list objectwith open('test.txt', 'r') as f:a = json.loads(f.read())
outfile = open('outfile.txt', 'w') # open a file in write modefor item in list_to_persistence: # iterate over the list itemsoutfile.write(str(item) + '\n') # write to the fileoutfile.close() # close the file