954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Fileinput from files ignoring incorrect ones.

0
By Tony Veijalainen on Sep 21st, 2011 1:29 am

Learned about hook functions for a thread having problem with bad user input from the documentation. This is result which skips bad inputs.

import fileinput

#prepare empty file to open in case of IOerror
open('dummy', 'w').close()

def hook_skip_IOerror(filename, mode):
        try:
            return open(filename, mode)
        except IOError:
            print("There was a problem with opening %s" % filename)
            return open('dummy', mode)          

def process(searchterm):
    for line in fileinput.input(openhook=hook_skip_IOerror):
        if line is not None:
           num_matches = line.count(searchterm)
           if num_matches:                     # a nonzero count means there was a match
               print "found '%s' %d times in %s on line %d." % (searchterm, num_matches,
                   fileinput.filename(), fileinput.filelineno())

process('=')

Actually the line 15, while does not disturb, does nothing, as it is from before I realized I must always return file object. So it should be removed and rest of the function unindented for clean code.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Ok now that i've found your code, as I am a newb, would you mind explaining the openhook concept a bit as i don't full grasp it?

Skrell
Light Poster
29 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

What you catched from fileinput documentation?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

What does process('=') do ? And what does opening a 'dummy' file accomplish? Does it allow the function to continue by replacing the unreadable file with a dummy one that IS readable ?

Skrell
Light Poster
29 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

The action in function is based in your own code only made it run as you gave no definition off searchterm. So of course it tells accurances of = in line making it easy to test the script with bunch of Python files as arguments.

You did read fileinput documentation? I use the mention that empty files are opened and closed immediately, to pass the bad file with warning message from program

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: