I'm trying fileinput to read some compressed files, and I tried the following three methods, however, none of them really works.
file=os.join.path(path+filename)
for i,line in enumerate(fileinput([file], openhook=gzip.open)):
for i,line in enumerate(fileinput.input(openhook=fileinput.hook_compressed(file1,'r'))):
for i,line in enumerate(fileinput.FileInput(openhook=fileinput.hook_compressed(file1,'r'))):
For the first command, errors are like:
'module' object is not callable
For the third command, errors like:
Traceback (most recent call last):
File "read_file.py", line 15, in <module>
for i,line in enumerate(fileinput.input(openhook=fileinput.hook_compressed(file1,'r'))):
File "/share/lib/python2.6/fileinput.py", line 103, in input
_state = FileInput(files, inplace, backup, bufsize, mode, openhook)
File "/share/lib/python2.6/fileinput.py", line 230, in __init__
raise ValueError("FileInput openhook must be callable")
ValueError: FileInput openhook must be callable
I don't understand why openhook cannot be callable here?
Can anyone help me with this? thx
You should pass in the function object as the hook parameter, not call the function.
In more detail, if
function
is a function object (something somebody declared withdef
orlambda
), thencalls the function, and stores the result in
variable
. When instead you sayyou assign (a reference to) the function object to
variable
, so that you now can useas effectively a synonym for
This usage is relatively rare otherwise, but definitely the norm for hook variables (and indeed, the whole point of hook variables - they offer a "hook" where you can place your own function inside the flow of another class or function. They are alse known as callbacks, if this term should be more familiar).