Wednesday, March 10, 2010

Python Script to create Python Scripts

This little python script is inspired from its ruby script equivalent found in the book Practical Ruby for System Administration, where the author created a script to create ruby scripts. The point of the script is to automate the tedious steps of creating a file, inserting the SHE-BANG line and making it executable. The original ruby version is more elegant, but I'm not that experienced in Python; so maybe there's room for improvements... suggestions welcome.

#!/usr/bin/env python

import sys,os,tkFileDialog

try:    path = sys.argv[1]
except: path = tkFileDialog.asksaveasfilename()

if not path:
    print "must specify a filename"
    exit(1)

f = open(path, "w")
f.write("#!/usr/bin/env python\n\n")
os.fchmod(f.fileno(), 0755)
f.close

os.system("/usr/bin/open " + path)



No comments: