[Tutor] fileinput problem

Kevin Reeder reederk at comcast.net
Sat Jun 25 07:21:52 CEST 2005

I'm getting unexpected behaviour from this module. My script is
meant to count the number of occurences of a term in one or more


import sys, string, fileinput

searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:]

for line in fileinput.input():
	num_matches = string.count(line, searchterm)
	if num_matches:
		print "found '%s' %i times in %s on line %i." % (searchterm,
			num_matches, fileinput.filename(), fileinput.filelineno())


When I run at the command line, this error message appears:

$ python ./Python/fileinput.py for ./Python/*.py

Traceback (most recent call last):
  File "./Python/fileinput.py", line 1, in ?
    import sys, string, fileinput
  File "./Python/fileinput.py", line 6, in ?
    for line in fileinput.input():
AttributeError: 'module' object has no attribute 'input'


I'm stumped. Any ideas?


More information about the Tutor mailing list