[Tutor] WTF?!?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu May 20 20:59:24 EDT 2004

On Thu, 20 May 2004, Kirk Bailey wrote:

> Here's the old script:
> #!/usr/local/bin/python
> #
> #
> f1=open("count",'r')
> count=int(f1.readline())
> f1.close()
> count=count+1
> result=str(count)
> print result
> f1=open("count",'w')
> f1.write(result+"\r\n")
> f1.close

Hi Kirk,

There's a bug here: you need to call f1.close(), with parentheses.  Even
methods with no arguments need parentheses to fire off properly.

(We may not be seeing this bug only because the code is inadvertantly
flushing the file buffer, but we can't depend on our luck to last.

> Hmmm, had something like this before. Turns out that when you run a
> program via the web server, it is now defaulting to the root dir for
> that domain name (criter holds several domains, each with it's own
> seperate directory tree structure including a web cgi-bin).

Yes --- CGI scripts shouldn't assume where the current working directory
is when they are executed.  Apache mentions that absolute paths should be
used for executable names:


and I think that the same caveat should apply to the names of data files

The change in behavior probably has nothing to do with Python or FreeBSD,
but is more likely due to changes in your web server configuration.  I
can't find anything in the CGI spec that defines what the current working
directory is, I suspect it's "undefined" in the sense that a CGI script
should not depend on the current working directory.

So instead of changing things to:


I'd recommend going one step further, and make it absolutely qualified.
If your DocumentRoot is under /var/www, then something like:

    f1 = open("/var/www/cgi-bin/count", "r")

would work.

Hope this helps!

More information about the Tutor mailing list