Advertisements

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.

Advertisements

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.

On May 12, 2:53 pm, MRAB <> 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.

walterbyrd wrote:
> On May 12, 2:53 pm, MRAB <> 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".

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'

(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.

On May 12, 4:39 pm, 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.

On May 12, 6:12 pm, Terry Reedy <> 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.

On Tue, May 12, 2009 at 11:47 PM, walterbyrd <> wrote:
> On May 12, 6:15 pm, norseman <> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!