[Tutor] Attempting to install Boa Constructor - User Error Detected

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Aug 19 00:00:21 CEST 2004

On Wed, 18 Aug 2004, Bob Gailer wrote:

> [snip]
> It has often troubled me that programs (python included) report "can't open
> file" when it is more accurate and useful to report "can't find file".

[meta: skip this message if you're not interested in how Python's C
implementation works.]

Hi Bob,

Hmmm...ok.  Out of curiosity, let's see how Python opens up source files.
Thank goodness Python is an open-source project!  *grin*

The relevant source-code opening code for the Python interpreter lives in

/*** modules/main.c ***/
if (filename != NULL) {
    if ((fp = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "%s: can't open file '%s'\n",
                argv[0], filename);
        return 2;

Ok, bingo.  So that's where that message is coming from in Python.  We can
make that error message a lot more informative by using C's perror()  or
strerror() functions: both functions can look at the standard "errno"
error integer, and give a nice description of what really happened.

Here's one way to make the error message more informative:

if ((fp = fopen(filename, "r")) == NULL) {
    /* dyoo: added call to strerror to see
       if we can improve the state of
       error messages*/
    fprintf(stderr, "%s: can't open file '%s': %s\n",
            argv[0], filename, strerror(errno));
    return 2;

With this modification, the Python interpreter now says:

[dyoo at shoebox python]$ bin/python filethatdoesnotexist.py
bin/python: can't open file 'filethatdoesnotexist.py': No such file or

Better!  *grin*

> So I recommend that in a future version of Python we fix this message.

Ok, I'll send a patch over to SourceForge.  Let's see if it gets in!

More information about the Tutor mailing list