Created
October 3, 2012 12:21
-
-
Save DanielSzoska/3826652 to your computer and use it in GitHub Desktop.
preserve type of newlines in textfile while doing something with it
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
# open textfile | |
filename = 'text.txt' | |
with open(filename, 'rU') as f: | |
lines = f.readlines() | |
newline = f.newlines | |
if not newline: | |
raise IOError, "no newlines in %s, possible no textfile" % filename | |
if isinstance(newline, tuple): | |
raise IOError, "more than one type of newlines in %s" % filename | |
# now do something with lines ... | |
# save textfile while preserving original newlines | |
with open('text_processed.txt', 'wb') as f: | |
f.write(newline.join(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment