wo_shi_big_stomach wrote: > Newbie to python writing a script to recurse a directory tree and delete > the first line of a file if it contains a given string. I get the same > error on a Mac running OS X 10.4.8 and FreeBSD 6.1. > > Here's the script: > > # start of program > > # p.pl - fix broken SMTP headers in email files > # > # recurses from dir and searches all subdirs > # for each file, evaluates whether 1st line starts with "From " > # for each match, program deletes line > > import fileinput > import os > import re > import string > import sys > from path import path > > # recurse dirs > dir = path(/home/wsbs/Maildir) > for f in dir.walkfiles('*'): > # > # test: > # print f
Are you absolutely sure that f list doesn't contain any path to directory, not file? Add this: f = filter(os.path.isfile, f) and try one more time. > # > # open file, search, change if necessary, write backup > for line in fileinput.input(f, inplace=1, backup='.bak'): > # check first line only > if fileinput.isfirstline(): > if not re.search('^From ',line): > print line.rstrip('\n') > # just print all other lines > if not fileinput.isfirstline(): > print line.rstrip('\n') > fileinput.close() > # end of program -- HTH, Rob -- http://mail.python.org/mailman/listinfo/python-list