How to write dos newline in unix?

Peter Otten __peter__ at web.de
Wed Jun 9 16:30:17 EDT 2004


Inyeol Lee wrote:

> Is there any simple way to dos-style newline (\r\n) in *nix environment,
> not by writing "\r" explicitly? What I'm trying to do is;

>>> class Translate:
...     def __init__(self, dest):
...             self.dest = dest
...     def write(self, s):
...             self.dest.write(s.replace("\n", "\r\n"))
...     def __getattr__(self, name):
...             return getattr(self.dest, name)
...
>>> f = Translate(file("tmp.txt", "w"))
>>> print >> f, "so what"
>>> print >> f, "will this print?"
>>> f.close()
>>> file("tmp.txt").read()
'so what\r\nwill this print?\r\n'

Simple enough?

Peter




More information about the Python-list mailing list