Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Convert UNIX formated text files to DOS formated?

Reply
Thread Tools

Convert UNIX formated text files to DOS formated?

 
 
walterbyrd
Guest
Posts: n/a
 
      05-12-2009
I have about 150 unix formated text files that I would like to convert
to dos formated.

I am guessing that I loop though each file in the directory, read each
line and conver the last character, then save to a file with the same
name in another directory.

I am not really sure what I convert the last charactor to.
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-12-2009
walterbyrd schrieb:
> I have about 150 unix formated text files that I would like to convert
> to dos formated.
>
> I am guessing that I loop though each file in the directory, read each
> line and conver the last character, then save to a file with the same
> name in another directory.
>
> I am not really sure what I convert the last charactor to.


Use recode.

Diez
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      05-12-2009
walterbyrd wrote:
> I have about 150 unix formated text files that I would like to convert
> to dos formated.
>
> I am guessing that I loop though each file in the directory, read each
> line and conver the last character, then save to a file with the same
> name in another directory.
>
> I am not really sure what I convert the last charactor to.


The quickest and OS-agnostic way would be:

text = open(path, "U").read()
text = text.replace("\n", "\r\n")
open(path, "wb").write(text)

That way it doesn't matter if the text file is already dos formatted.
 
Reply With Quote
 
walterbyrd
Guest
Posts: n/a
 
      05-12-2009
On May 12, 2:53*pm, MRAB <(E-Mail Removed)> wrote:
> walterbyrd wrote:
> > I have about 150 unix formated text files that I would like to convert
> > to dos formated.

>
> > I am guessing that I loop though each file in the directory, read each
> > line and conver the last character, then save to a file with the same
> > name in another directory.

>
> > I am not really sure what I convert the last charactor to.

>
> The quickest and OS-agnostic way would be:
>
> * * *text = open(path, "U").read()
> * * *text = text.replace("\n", "\r\n")
> * * *open(path, "wb").write(text)
>
> That way it doesn't matter if the text file is already dos formatted.



Thanks, I am not familiar with the "U" here is how I did it:

------------
import os

for file in os.listdir('.'):
infile = open(file,'r')
outfile = open( 'new_' + file, 'w')
for line in infile:
line = line.rstrip() + '\r\n'
outfile.write(line)
infile.close()
outfile.close()
------------

More code, probably slower, but it worked.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      05-12-2009
walterbyrd wrote:
> On May 12, 2:53 pm, MRAB <(E-Mail Removed)> wrote:
>> walterbyrd wrote:
>>> I have about 150 unix formated text files that I would like to convert
>>> to dos formated.
>>> I am guessing that I loop though each file in the directory, read each
>>> line and conver the last character, then save to a file with the same
>>> name in another directory.
>>> I am not really sure what I convert the last charactor to.

>> The quickest and OS-agnostic way would be:
>>
>> text = open(path, "U").read()
>> text = text.replace("\n", "\r\n")
>> open(path, "wb").write(text)
>>
>> That way it doesn't matter if the text file is already dos formatted.

>
>
> Thanks, I am not familiar with the "U" here is how I did it:
>
> ------------
> import os
>
> for file in os.listdir('.'):
> infile = open(file,'r')
> outfile = open( 'new_' + file, 'w')
> for line in infile:
> line = line.rstrip() + '\r\n'
> outfile.write(line)
> infile.close()
> outfile.close()
> ------------
>
> More code, probably slower, but it worked.


That will also strip any whitespace off the end of the lines.

FYI, if you run it on Windows then the resulting lines will end with
"\r\n\n" because the output file is opened in text mode, which will
write any "\n" as "\r\n".
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      05-13-2009
walterbyrd wrote:
> I have about 150 unix formated text files that I would like to convert
> to dos formated.


Are you sure you need to do that? Most Windows programs (including
Python) are happy reading text files with just \n for line endings.

 
Reply With Quote
 
norseman
Guest
Posts: n/a
 
      05-13-2009
walterbyrd wrote:
> I have about 150 unix formated text files that I would like to convert
> to dos formated.
>
> I am guessing that I loop though each file in the directory, read each
> line and conver the last character, then save to a file with the same
> name in another directory.
>
> I am not really sure what I convert the last charactor to.

===================
Subject line says UNIX to DOS

I hope that means you are using a UNIX machine.

addcr.scr
========
#!/bin/bash
#
cat $1 | sed s/$/$'\x0d'/ >$1.cr
#
# end of file
========
I have utils on MSDOS that add the cr to get the crlf sequence
I wrote it for use in the rare occasion. (my definition of rare.




rmcr.scr
========
#!/bin/sh
# rmcr.scr
# removes <cr> from text files
# Date: Feb. 2004
# by: SLT
# : file dressed up Dec. 2004
#
# a literal <cr> follows first slash following the s.
#
i=$1
i1=${i%%.*}
echo $i1
cat $1 | sed s/^M// >$i1._cr
#
# end of file
===========
The ^M will needs to be entered at 'type in' time.
In vi it is done by pressing CTRL-V CTRL-M in sequence
DO NOT CUT n PASTE that line!!!
I have never been able to use the $'\x0d' in this file.
The MSDOS programs don not run on Linux, per se. 'Ya Think'


crlf \r \n x'0D' x'0A' CTRL-M CTRL-J
carriage return, line feed keep the pair in the order shown.



to use in UNIX:

(be in a bash shell)
bash
for f in *.txt; do addcr.scr $f; done # going to MSDOS

for f in *.txt; do rmcr.scr $f; done # returning from MSDOS

if you run these on a binary - the binary is destroyed!!!


If you are in MSDOS, I have no help.
Well - see if you can find and download FILT.EXE (ver 1.0)
from the 1980 something era
FILT <Enter> will present the helpfiles
It's what I use. I set up batch (.BAT) files to expedite my desires.


Best of Luck


Steve
 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      05-13-2009
On May 12, 4:39*pm, walterbyrd <(E-Mail Removed)> wrote:
> I have about 150 unix formated text files that I would like to convert
> to dos formated.
>
> I am guessing that I loop though each file in the directory, read each
> line and conver the last character, then save to a file with the same
> name in another directory.
>
> I am not really sure what I convert the last charactor to.


Python source includes tool for this: lfcr.py

http://svn.python.org/view/python/tr...py?view=markup

Depending on how python was installed on your system you might already
have this script on your system. (FYI there's also a crlf.py script
in the same directory too.)

HTH
~Simon
 
Reply With Quote
 
walterbyrd
Guest
Posts: n/a
 
      05-13-2009
On May 12, 6:12*pm, Terry Reedy <(E-Mail Removed)> wrote:

> Are you sure you need to do that? *Most Windows programs (including
> Python) are happy reading text files with just \n for line endings.


These files will be looked at by some non-technical people. I am sure
these people will just click on the icons, and the file will come up
in notepad.

But, you are correct, I know that wordpad, and notepad++, and many
other apps will read the UNIX formated files just fine. IMO: notepad
is total junk, but it's still the default text editor on windows.
 
Reply With Quote
 
walterbyrd
Guest
Posts: n/a
 
      05-13-2009
On May 12, 6:15*pm, norseman <(E-Mail Removed)> wrote:

> Subject line says UNIX to DOS
>
> I hope that means you are using a UNIX machine.
>


I should have mentioned, I am working in an environment that is very
restrictive about what I can put on my XP desktop. I can not put
python, or even notepad++, on my desktop. But, I do have cygwin.

As you probably know, cygwin formats to UNIX.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert currency formated string back into decimal =?Utf-8?B?R3JlZyBH?= ASP .Net 1 11-03-2007 04:48 PM
how to convert dos file to unix file in eclipse david wolf Java 5 04-21-2006 06:53 AM
Reading Mac / Unix / DOS text files January Weiner Perl Misc 23 02-27-2006 08:25 AM
Dos vs Unix style text files Dave Moore C++ 8 02-12-2005 04:01 PM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM



Advertisments