File processing

Skip Montanaro skip at pobox.com
Mon Jul 9 23:41:24 EDT 2001


    Chris> I'm trying to write a script that will open a file, delete the
    Chris> first line, and then save the file with the same name.  Can
    Chris> someone please point me in the right direction?  Thanks!

Well, you've seen several Python examples and a Matlab example.  I usually
use the shell for these sorts of quick-n-dirty file manipulations:

    i=`wc -l <somefile`
    i=`expr $i - 1`
    tail -$i somefile > somefile.short

Put that in a loop and you're all set:

    for f in `whole buncha files` ; do
        i=`wc -l <$f`
	i=`expr $i - 1`
	tail -$i $f > $f.tmp
	# cp -p $f $f.save		# if you're paranoid
	mv $f.tmp $f
    done

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list