fileinput usage with openbook=hook_compressed

2019-08-10 17:24发布

问题:

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

回答1:

You should pass in the function object as the hook parameter, not call the function.

for i, line in enumerate(fileinput.input(openhook=fileinput.hook_compressed)):
    sys.stdout.write("%-6i %s" % (i, line))

In more detail, if function is a function object (something somebody declared with def or lambda), then

variable = function()

calls the function, and stores the result in variable. When instead you say

variable = function

you assign (a reference to) the function object to variable, so that you now can use

 variable()

as effectively a synonym for

function()

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).