os.path.* routine problems

David Nedrow dnedrow at usa.net
Thu May 3 12:40:47 EDT 2001


I'm attempting to fix up a couple of small utilities that are part of
sgmltools-lite, but am running into my usual Python problem - I don't know
much about it.

Here is the original segment:

try:
    pipe = os.popen("/bin/sh -c sgmlwhich >/dev/null 2>&1", "r")
    retval = string.strip(pipe.readline())
    if retval != '' and pipe.close() == 0:
        ETCSGMLCATDIR = retval
except:
    pass


This may have worked for older versions of RedHat, but beginning with 7.1 a
number of changes have been made that require the logic to be modified.
Basically, the utility needs to attempt to run sgmlwhich. Depending on the
version of sgmlwhich, it may return a pointer to a directory or a
configuation file. If the return is a directory, ETCSGMLCATDIR is set to
that directory. If sgmlwhich returns a filename, the utility needs to parse
the config file and store the KEY=VALUE pairs. One of these will be
SGML_CATALOGS_DIR. The value for this key should then be assigned to
ETCSGMLCATDIR.

The problem I am having is that none of my checks seems to be returning true
in the if-elif statement. Also, I had to remove the redirects from the popen
cmd, otherwise I did not receive the output of the cmd.


ETCSGMLCATDIR = "/etc/sgml"
import os, string
try:
    cmd = "/bin/sh -c sgmlwhich"
    retval = string.strip(os.popen(cmd).readline())
    if os.path.isdir(retval):
        ETCSGMLCATDIR = retval

    elif os.path.isfile(retval):
        entries = {}
        for line in open(retval, 'r').readlines():
            print line
            left, right = string.split(line, "=")
            if left!='' and right!='':
                entries[right] = [left]
        ETCSGMLCATDIR = entries['SGML_CATALOGS_DIR']

except:
    pass


Any pointers on what I doing wrong would be appreciated.

-David




More information about the Python-list mailing list