Created
May 17, 2012 11:01
-
-
Save roskakori/2718161 to your computer and use it in GitHub Desktop.
Python essentials
This file contains 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
''' | |
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