os.path.* routine problems

Lutz Neugebauer lne at hrz.tu-chemnitz.de
Thu May 3 15:33:57 EDT 2001


On Thu, 3 May 2001, David Nedrow wrote:

> 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.
if you remove try/except yuo get the exception thrown caused by the
comment line in /etc/sgml/sgml.conf:

# /etc/sgml/sgml.conf conformant with LSB

Traceback (innermost last):
  File "sgml.py", line 14, in ?
    (left, right) = string.split(line, "=")
ValueError: unpack list of wrong size


try this:

        for line in open(retval, 'r').readlines():
            print line
            list = string.split(line, "=")
            if len(list) == 2:
                entries[list[0]] = list[1]


Lutz

-- 
Lutz Neugebauer
Lutz.Neugebauer at informatik.tu-chemnitz.de





More information about the Python-list mailing list