[Tutor] path issues

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu May 1 16:18:01 2003


On Thu, 1 May 2003, Kirk Bailey wrote:

> ok, got a code problem.
>   os.remove('./lists/' + mylist)  # this deletes the subscriber file.
>   os.remove('./lists/' + mylist + '.*')   # remove all files with this
>
> blows out the script. Current dir is where the script is, the web
> cgi-bin, and the /lists dir is immediately under it./ Produces this error:
>
> Traceback (innermost last):
>    File "/www/www.tinylist.org/cgi-bin/TLlistkill2.py", line 172, in ?
>      os.remove('./lists/' + mylist + '.*')	# remove all files...
> OSError: [Errno 2] No such file or directory: './lists/foolist.*'


Hi Kirk,


You may want to set things up so that the script does not automatically
assume the location of the 'lists' directory.  How about making a
'configuration' file called 'TLListConfig.py'; it could have the
following:


### TLListConfig.py
## Define the absolute path of the lists directory:

## commented out; it's a bit dangerous to have user data in cgi-bin
## LISTS_DIR = "/www/www.tinylist.org/cgi-bin/lists"
LISTS_DIR = "/var/www.tinylist.org/lists"
###


In your TLlistkill2 program, or anywhere else in the mailing list system,
you can use:

###
os.remove(os.path.join(TLListConfig.LISTS_DIR, mylist))
###


The advantage of this is that having a separate configuration file is that
it allows your other users to easily relocate 'lists' to somewhere more
isolated: Rather than have your users modify every place where './lists'
is being used, it might be easier to centralize that variable in a fairly
readable configuration file.


(I often feel really nervous about CGI programs that modify or write files
into the cgi-bin directory.  My security spidey sense solicites me to
encourage you to make it easy to relocate the 'lists' directory somewhere
less dangerous.)



> ok, is it simply that the language will not permit relative addressing
> in the os module? It sure does in several others!

Sean and Magnus mentioned that wildcards aren't natively supported by
os.remove() -- you'll want to use the 'glob' module to handle them.




Good luck to you!