A Full Example: dos2unix.py

A Full Example: dos2unix.py

Let’s bring this whole thing home and look at a full example of how to read and write to a file. The following is a dos2unix like tool that will convert a file that contains line endings of \r\n to \n.

This tool is broken up into three major sections. The first is str2unix(), which converts a string from \\r\\n line endings to \\n. The second is dos2unix(), which converts a string that contains \r\n characters into \ndos2unix() calls str2unix(). Finally, there’s the __main__ block, which is called only when the file is executed as a script. Think of it as the main function found in other programming languages.