473,382 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Printing unix Line endings from Windows.

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,

Dec 4 '06 #1
6 3920
Ant wrote:
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
Dec 4 '06 #2
Ant

Larry Bates wrote:
Ant wrote:
....
Is there any way of doing this without having to post-process the file
in binary mode (a-la the crlf.py script)
....
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 ...

Dec 5 '06 #3
Ant wrote:
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>

Dec 5 '06 #4

Ant wrote:
Larry Bates wrote:
Ant wrote:
...
Is there any way of doing this without having to post-process the file
in binary mode (a-la the crlf.py script)
...
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 ...
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

Dec 5 '06 #5
Ant

John Machin wrote:
Ant wrote:
....
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.
....
| >>'\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,

Dec 5 '06 #6
Ant wrote:
Is it worth me submitting a patch to fileinput which can take an
optional write mode parameter?
absolutely.

</F>

Dec 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jay O'Connor | last post by:
I'm doing some Python CGI programming under Windows (Win95) but my CGIs need to run on Linux. If I try to write and save the files in IDLE, they get saved in DOS format and won't run on the Linux...
15
by: ajikoe | last post by:
Hello, I use windows notepad editor to write text. For example I write (in d:\myfile.txt): Helo World If I open it with python: FName = open(d:\myfile.txt,'r')
4
by: Fuzzyman | last post by:
Hello all, I'm handling some text files where I don't (necessarily) know the encoding beforehand. Because I use regular expressions to parse the text I *must* decode UTF16 encoded text...
18
by: Fuzzyman | last post by:
Hello all, I'm trying to detect line endings used in text files. I *might* be decoding the files into unicode first (which may be encoded using multi-byte encodings) - which is why I'm not...
17
by: Franz Steinhaeusler | last post by:
Hello, I need a regularexpression, which trims trailing whitespaces. While with unix line endings, it works; but not with Window (Dos) CRLF's: >>> import re >>> retrailingwhitespace =...
1
by: Ben | last post by:
Hi, I have a python script on a unix system that runs fine. I have a python script on a windows system that runs fine. Both use tabs to indent sections of the code. I now want to run them on the...
3
by: towers | last post by:
Hi I'm probably doing something stupid but I've run into a problem whereby I'm trying to add a csv file to a zip archive - see example code below. The csv just has several rows with carriage...
16
by: mazwolfe | last post by:
Someone recently asked about reading lines. I had this code written some time ago (part of a BASIC-style interpreter based on H. Shildts in Art of C) to read a file with the lines ended in any...
4
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.