On 11/23/06 6:15 AM, Rob Wolfe wrote:
> 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.

Sorry, no joy. Printing f then produces:

rppp
rppppp
rppppp
rpppr
rppppp
rpppP
rppppp
rppppp

which I assure you are not the filenames in this directory.

I've tried this with f and f.name. The former prints the full pathname
and filename; the latter prints just the filename. But neither works
with the fileinput.input() call below.

I get the same error with the filtered mod as before:

  File "./p", line 23, in ?
    for line in fileinput.input(f, inplace=1, backup='.bak'):

Thanks again for info on what to feed fileinput.input()



> 
>>      #
>>      # 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
> 

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to