Printing unix Line endings from Windows.

A

Ant

Hi all,

I've got a problem here which has me stumped. I've got a python script
which does some text processing on some files and writes it back out to
the same file using the fileinput module with inplace set to True.

The script needs to run from Windows, but the files need to be written
with Unix line endings.

Is there any way of doing this without having to post-process the file
in binary mode (a-la the crlf.py script)

Cheers,
 
L

Larry Bates

Ant said:
Hi all,

I've got a problem here which has me stumped. I've got a python script
which does some text processing on some files and writes it back out to
the same file using the fileinput module with inplace set to True.

The script needs to run from Windows, but the files need to be written
with Unix line endings.

Is there any way of doing this without having to post-process the file
in binary mode (a-la the crlf.py script)

Cheers,
You can write to a new file and create your own line endings.
When done, delete the original file and rename the output file.

-Larry
 
A

Ant

Larry said:
Ant wrote: .... ....
You can write to a new file and create your own line endings.
When done, delete the original file and rename the output file.

How can I create my own line endings? I've tried setting os.linesep =
"\n", (and to \x0a). I've tried things like:

print "xxx yyy \n",
print "xxx uuu \x0a",
filehandle.write("xxx \n")
filehandle.write("xxx \x0a")

and all of these give me a nice windows-style crlf!

Surely there must be a way to do this ...
 
F

Fredrik Lundh

Ant said:
How can I create my own line endings? I've tried setting os.linesep =
"\n", (and to \x0a). I've tried things like:

print "xxx yyy \n",
print "xxx uuu \x0a",
filehandle.write("xxx \n")
filehandle.write("xxx \x0a")

and all of these give me a nice windows-style crlf!

Surely there must be a way to do this ...

endline normalization is done by the file object, on the way out. to
switch this off, open the output file in binary mode ("wb").

</F>
 
J

John Machin

Ant said:
How can I create my own line endings? I've tried setting os.linesep =
"\n", (and to \x0a). I've tried things like:

print "xxx yyy \n",
print "xxx uuu \x0a",
filehandle.write("xxx \n")
filehandle.write("xxx \x0a")

and all of these give me a nice windows-style crlf!

Surely there must be a way to do this ...

and there is: open your output file in binary mode; then it won't
convert every \n to \r\n.

writing:
| >>> f = open('unixlf.txt', 'wb')
| >>> f.write('foo\n')
| >>> f.write('bar\n')
| >>> f.close()

checking:
| >>> f = open('unixlf.txt', 'rb')
| >>> x = f.read()
| >>> x
| 'foo\nbar\n'
| >>> len(x)
| 8

BTW:
| >>> '\n' is '\x0a'
| True

HTH,
John
 
A

Ant

John said:
Ant wrote: ....

and there is: open your output file in binary mode; then it won't
convert every \n to \r\n. ....
| >>> '\n' is '\x0a'
| True

(and F wrote something very similar.)

Cheers guys. Shame that fileinput doesn't take an argument to specify
the write mode when 'inplace' is True, because it is otherwise makes
editing multiple files in place very simple.

Is it worth me submitting a patch to fileinput which can take an
optional write mode parameter?

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top