[Tutor] fileinput problem

Kent Johnson kent37 at tds.net
Sat Jun 25 12:41:01 CEST 2005


Kevin Reeder wrote:
> 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
> files.
> 
> ---
> 
> 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?

You named your program fileinput.py, so when you import fileinput you are getting your own program again instead of the library module. Change the name of your program and try again.

Kent



More information about the Tutor mailing list