[Fwd: directly executing a python script in Unix]

Fernando Pérez fperez528 at yahoo.com
Mon Feb 4 13:35:25 EST 2002


>> I've read once that Linux doesnt finds she-bang command scripts if
>> they end with something different from a newline - for example a
>> carriage return plut a newline (MS Windows / DOS convention). So it's
>> possible to write such a script under DOS, which is visually identical
>> but won't run under Linux.

Here's my python fix for that moronic problem (moronic of Dos designers, who 
thought that two characters per line ending were a good idea when there 
already was a perfectly viable convention to follow, namely that of Unix. 
Perhaps they were following a VMS convention or somesuch, I don't know).

Call this tounix.py, put in your path and use as needed even on a whole 
directory ("tounix.py *" will work):

[python]> cat tounix.py
#!/usr/bin/env python

"""Convert DOS or Macintosh text files to unix end-of-line convention.

Usage:
  tounix file1 file2 ...

A backup (file1~,file2~,etc.) of each file is made.
"""

import sys,os

files = sys.argv[1:]

if len(files) == 0:
    print __doc__
    sys.exit()

for file in files:
    original = open(file).read()
    bak = file+'~'
    os.rename(file,bak)
    new = open(file,'w')
    new.write('\n'.join(original.splitlines()))
    new.close()


PS. This is quick and dirty but works. If you want, you can spice it up with 
lots of error checks and whatnot. 

Cheers,

f



More information about the Python-list mailing list