Convert UNIX formated text files to DOS formated?

Discussion in 'Python' started by walterbyrd, May 12, 2009.

  1. walterbyrd

    walterbyrd Guest

    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.
    walterbyrd, May 12, 2009
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, May 12, 2009
    #2
    1. Advertising

  3. walterbyrd

    MRAB Guest

    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.
    MRAB, May 12, 2009
    #3
  4. walterbyrd

    walterbyrd Guest

    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.
    walterbyrd, May 12, 2009
    #4
  5. walterbyrd

    MRAB Guest

    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".
    MRAB, May 12, 2009
    #5
  6. walterbyrd

    Terry Reedy Guest

    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.
    Terry Reedy, May 13, 2009
    #6
  7. walterbyrd

    norseman Guest

    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
    norseman, May 13, 2009
    #7
  8. walterbyrd

    Simon Forman Guest

    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.


    Python source includes tool for this: lfcr.py

    http://svn.python.org/view/python/trunk/Tools/scripts/lfcr.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
    Simon Forman, May 13, 2009
    #8
  9. walterbyrd

    walterbyrd Guest

    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.
    walterbyrd, May 13, 2009
    #9
  10. walterbyrd

    walterbyrd Guest

    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.
    walterbyrd, May 13, 2009
    #10
  11. walterbyrd

    walterbyrd Guest

    Thanks for shell script code. That code may be just as efficient, or
    even more efficient, than python. But, to me, python is far more
    readable.

    ------------
    i=$1
    i1=${i%%.*}
    echo $i1
    cat $1 | sed s/^M// >$i1._cr
    -----------=


    ------------
    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()
    ------------
    walterbyrd, May 13, 2009
    #11
  12. -On [20090513 05:53], walterbyrd () wrote:
    >As you probably know, cygwin formats to UNIX.


    That entirely depends on how you install it.

    Anyway, I am sure the Cygwin repository has the tools dos2unix/unix2dos
    available for installation.

    --
    Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
    イェルーン ラウフロック ヴァン デル ウェルヴェン
    http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
    In short may I, directly and indirectly, offer benefit and happiness to all
    beings, may I secretly take upon myself the harm and suffering of all
    beings...
    Jeroen Ruigrok van der Werven, May 13, 2009
    #12
  13. 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
    >


    cygwin has u2d and d2u
    David Robinow, May 13, 2009
    #13
  14. walterbyrd

    walterbyrd Guest

    On May 13, 3:21 am, David Robinow <> wrote:
    > cygwin has u2d and d2u- Hide quoted text -


    Thank you, I did not know about those utilities, until now.
    walterbyrd, May 13, 2009
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. david wolf
    Replies:
    5
    Views:
    15,206
    Thomas Weidenfeller
    Apr 21, 2006
  2. Dave Moore

    Dos vs Unix style text files

    Dave Moore, Feb 10, 2005, in forum: C++
    Replies:
    8
    Views:
    6,662
    Ron Natalie
    Feb 12, 2005
  3. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    276
    Michele Dondi
    Jan 22, 2004
  4. Andrew
    Replies:
    23
    Views:
    295
    Tad McClellan
    May 9, 2004
  5. January Weiner

    Reading Mac / Unix / DOS text files

    January Weiner, Feb 21, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    309
    January Weiner
    Feb 27, 2006
Loading...

Share This Page