Esmail <ebo...@hotmail.com> wrote: > I am wondering if anyone is using python to write script files?
Yes! > Right now I have a bigg'ish bash/tcsh script that contain some grep/awk > command plus various files are processed and created, renamed and > moved to specific directories. I also write out some gnuplot scripts > that later get executed to generate .jpg images. Almost any script that contains a loop I convert into python. > In any case, the scripts are starting to look pretty hairy and I was > wondering if it would make sense to re-write them in Python. I am not > sure how suitable it would be for this. With python you get the advantages of a language with a very clear philosophy which is exceptionally easy to read and write. Add a few classes and unit tests to your scripts and they will be better than you can ever achieve with bash (IMHO). > I've looked around the web w/o much luck for some examples but come > short. Any comments/suggestions? Here is a short script I wrote to recover my slrn spool when my disk fills up. I could have written it in bash but I think it is much clearer as python, and doesn't shell out to any external commands. It searches through the spool directory looking for empty .minmax files. It then finds the lowest and highest numeric file name (all the files are numeric) and writes the minmax file with that. #!/usr/bin/python """ rebuild all the .minmax files for slrnpull after the disk gets full """ import os spool = "/var/spool/slrnpull" def main(): for dirpath, dirnames, filenames in os.walk(spool): if ".minmax" not in filenames: continue minmax_path = os.path.join(dirpath, ".minmax") if os.path.getsize(minmax_path) != 0: print "Skipping non empty %r" % minmax_path continue print dirpath digits = [ int(f) for f in filenames if f.isdigit() ] if not digits: digits = [0] digits.sort() start = digits[0] end = digits[-1] f = open(minmax_path, "w") f.write("%s %s" % (start, end)) f.close() print "done" if __name__ == "__main__": main() -- Nick Craig-Wood <n...@craig-wood.com> -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list