[Tutor] Appropriate use of None

Martin A. Brown martin at linux-ip.net
Sat Jan 1 12:32:03 EST 2022


Hello all,

> It should not be done because you should use "with" instead
> which will automatically close the file.

I'll second this motion.

> The more unnecessary tests you introduce the more potential
> for error.

And, I'll second that motion.

> try:
>    with open(...) as infile:
>         # process here
> except IOError:
>    print("File filefortest.txt cannot be opened")
> else:
>    print("File is closed")
> 

A person I worked with a decade ago used to say "my software 
inhabits a hostile universe" and when you have seen enough strange 
system configurations, errors, crashes and other failures, you can 
see why this mindset might prevail.

With that in mind, I have two things I would add to Alan's skeleton
suggestion, Manprit.

send status messagse to STDERR
------------------------------
is to send the status-like messages to STDERR, keeping STDOUT for 
your expected output.  Alternately, you could consider the logging 
module, but printing to STDERR is a perfectly normal technique.

say what happened when catching an exception (and continuing)
-------------------------------------------------------------
If you are catching an exception and continuing, mention what 
happened (unless you are correcting or deliberately swallowing the 
exception).  There's on old theme, thta matches with Alan's comment 
above.  "Don't catch any exception you're not prepared to handle."

So, consider something like this.

import sys


def do_stuff_with(f):
    try:
       with open(f) as infile:
           pass
    except IOError as err:
        print("Skipping file {}: {}".format(f, err), file=sys.stderr)
    else:
       print("File {} is closed".format(f), file=sys.stderr)


if __name__ == '__main__':
    for arg in sys.argv[1:]:
        do_stuff_with(arg)


Where I have used "pass", you would put your processing logic, to 
call another function, collect data for return (this function 
doesn't return anything) or simply perform the desired work inline.

When I run the above tiny program (which I called 
manprit-open-error.py) with 2 good input files and 3 different kinds 
of bad input files, I see the following produced to STDERR:

$ cat manprit-open-error.py | python3 manprit-open-error.py /dev/stdin readable-file.txt   /var/lib/ -  unreadablefile 
File /dev/stdin is closed
File readable-file.txt is closed
Skipping file /var/lib/: [Errno 21] Is a directory: '/var/lib/'
Skipping file -: [Errno 2] No such file or directory: '-'
Skipping file unreadablefile: [Errno 13] Permission denied: 'unreadablefile'

The above STDERR is useful for a user to understand what errors the 
program encountered while still letting your program try to succeed.

-Martin

P.S.  Some programs accept "-" as a filename meaning STDIN.  To my 
  understanding that is something that is an application choice, 
  which would require a bit of logic.  I've added that as a creature 
  comfort in some programs I have written, but not in the above toy 
  example.

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list