script in Linux vs Windows

Ian Kelly ian.g.kelly at gmail.com
Fri Oct 8 19:27:23 EDT 2010


On Fri, Oct 8, 2010 at 3:52 PM, <aurfalien at gmail.com> wrote:

> Hi all,
>
> Unsure how to deal with what appears to be \n vs \r issues.
>
> The following code works in Linux;
>
> o = open("axenfs.reg")
> n = open("axenfs2.reg", "a")
> while 1:
>  line = o.readline()
>  if not line: break
>  line = line.replace("dword:00000","dword:0000044e")
>  n.write(line)
> n.close()
>
> But in Windows, its one continues line with a bunch of squares in it.
>
> So I add + '\r\n' to my second to last line so it looks like;
>
> n.write(line + '\r\n')
>
> But I still get one continuous line with squares in it.
>
> Any ideas?  I've been googing all day to no avail.
>

Is the input file formatted the same way?  I would guess that the readline
call is reading the input file as one continuous line, so that the loop body
only runs once, and the '\r\n' only gets added to the very end of the file.

The simplest way to deal with this type of issue is to convert the file line
endings as needed using dos2unix and unix2dos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101008/0e9c2a25/attachment.html>


More information about the Python-list mailing list