End of line converter I recently wrote a python script to convert multiple files EOL's from Unix to DOS and vice versa. 

I am looking for tips to improve my code or if there is a better way of doing something that I have missed.

#!/usr/bin/env python3

import sys

def main():
    command, *filenames = sys.argv[1:]
    valid_commands = ['-d', '-u']
    sys.tracebacklimit = None

    if not command in valid_commands:
        error = """'{command}'
        Provide the following arguments -u|d file [file2] [file3] ...
        flags:
            -u : converts DOS to UNIX
            -d : converts UNIX to DOS
        example command:
            ./eol -u foo.py bar.py""".format(command=command)

        raise ValueError(error)
        sys.exit(1)

    if filenames:
        convert(filenames, command)
    else:
        print("&gt; no files to convert")

def convert(files, command):
    for file in files:
        text = open(file, 'r').read()

        with open(file, 'w') as current:
            if command == '-u':
                format = 'UNIX'
                current.write(text.replace('\r\n', '\n'))
            elif command == '-d':
                format = 'DOS'
                current.write(text.replace('\n', '\r\n'))

        print("&gt; converting file {filename} to {format} ...".format(
              filename=file, format=format))

if __name__ == "__main__":
    main()
The code in the post does not work, because the files are opened in text mode, and in text mode Python 3 translates newlines by default. To quote the Python documentation:

newline controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.


This means that the code in the post never gets to see the original line endings and so it does not behave as intended when run on Windows. (This makes me suspect that it has not been tested in all four configurations: Unix → DOS on Unix; DOS → Unix on Unix; Unix → DOS on Windows; DOS → Unix on Windows.)
In order to operate on the original line endings, you could open the file in binary mode (both for reading and writing), or open it in text mode but set newline='' so that newlines are not translated.