Skip to content

Instantly share code, notes, and snippets.

@roskakori
Created May 17, 2012 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roskakori/2718161 to your computer and use it in GitHub Desktop.
Save roskakori/2718161 to your computer and use it in GitHub Desktop.
Python essentials
'''
Python 2.x essentials.
This requires Python 2.5 or later.
'''
from __future__ import with_statement
import codecs
import os
# Write a text file containing a couple of lines.
with codecs.open('test.txt', 'wb', 'iso-8859-15') as testFile:
testFile.write(u'first line')
testFile.write(os.linesep)
testFile.write(u'second line with some more words')
testFile.write(os.linesep)
# Read a file line by line.
with codecs.open('test.txt', 'rb', 'iso-8859-15') as testFile:
for lineNumber, line in enumerate(testFile, start=1):
print '%d: %r' % (lineNumber, line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment