Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106461 > unrolled thread

Request Help With Function

Started byWildman <best_lay@yahoo.com>
First post2016-04-04 14:42 -0500
Last post2016-04-04 19:38 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Request Help With Function Wildman <best_lay@yahoo.com> - 2016-04-04 14:42 -0500
    Re: Request Help With Function Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-04 13:54 -0600
      Re: Request Help With Function Wildman <best_lay@yahoo.com> - 2016-04-04 19:26 -0500
    Re: Request Help With Function MRAB <python@mrabarnett.plus.com> - 2016-04-04 21:02 +0100
      Re: Request Help With Function Wildman <best_lay@yahoo.com> - 2016-04-04 19:38 -0500

#106461 — Request Help With Function

FromWildman <best_lay@yahoo.com>
Date2016-04-04 14:42 -0500
SubjectRequest Help With Function
Message-ID<-JSdnXtfvv0oXp_KnZ2dnUU7-evNnZ2d@giganews.com>
I am working on a Linux gui program where I want to be able
to click a Help button and open a man page using a viewer.
I wrote a search function that can be called several times,
if needed, with different arguments.  I wrote a test program
that tries to open the Bash man page in a terminal and will
display a message box if the search fails.  It passes the
system path, terminal emulators and command line arguments
to the search function.  I appears that you can't pass a list
to a function so I am passing the arguments as strings and then
converting them to lists for parsing in the search function.

When I run the test program, I get this error:

Traceback (most recent call last):
  File "./man.py", line 38, in <module>
    launch_help()
  File "./man.py", line 10, in launch_help
    if search(pathlist, executelist, commandlist):
  File "./man.py", line 27, in search
    subprocess.Popen(command)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
TypeError: execv() arg 2 must contain only strings

I have not been able to figure out why I'm getting the error.
Any help would be appreciated.  Below is the complete code for
the test program:

#!/usr/bin/env python

import os, subprocess, tkMessageBox

def launch_help():
    pathlist = os.environ["PATH"]
    executelist = "xvt,xfce4-terminal"
    commandlist = "-e,man bash"
    if search(pathlist, executelist, commandlist):
        return None
    message = "Open a terminal and enter:  man bash"
    tkMessageBox.showinfo("Help", message)

def search(pathlist, executelist, commandlist):
    pathlist = pathlist.split(":")
    executelist = executelist.split(",")
    commandlist = commandlist.split(",")
    done = False
    for path in pathlist:
        for execute in executelist:
            target = path + "/" + execute
            if os.path.isfile(target):
                done = True
                command = [target, commandlist]
                subprocess.Popen(command)
            if done:
                break
        if done:
            break
    if done:
        return True
    else:
        return False


launch_help()

-- 
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!

[toc] | [next] | [standalone]


#106462

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-04-04 13:54 -0600
Message-ID<mailman.31.1459799740.32530.python-list@python.org>
In reply to#106461
On Mon, Apr 4, 2016 at 1:42 PM, Wildman via Python-list
<python-list@python.org> wrote:
>     commandlist = commandlist.split(",")

commandlist is a list.

>                 command = [target, commandlist]
>                 subprocess.Popen(command)

This is passing a list containing two elements: the first is a string,
and the second is a list of strings. You should just pass a list of
strings. Probably you wanted to do this:

    command = [target] + commandlist

[toc] | [prev] | [next] | [standalone]


#106479

FromWildman <best_lay@yahoo.com>
Date2016-04-04 19:26 -0500
Message-ID<VKGdnWLPwMyrm57KnZ2dnUU7-WGdnZ2d@giganews.com>
In reply to#106462
On Mon, 04 Apr 2016 13:54:56 -0600, Ian Kelly wrote:

> On Mon, Apr 4, 2016 at 1:42 PM, Wildman via Python-list
> <python-list@python.org> wrote:
>>     commandlist = commandlist.split(",")
> 
> commandlist is a list.
> 
>>                 command = [target, commandlist]
>>                 subprocess.Popen(command)
> 
> This is passing a list containing two elements: the first is a string,
> and the second is a list of strings. You should just pass a list of
> strings. Probably you wanted to do this:
> 
>     command = [target] + commandlist

Thank you, that fixed the problem.  It did occur to me that the
problem was in how "command" was put together but I wasn't sure.
A rookie mistake from a rookie.  Whoda thunk it?

-- 
<Wildman> GNU/Linux user #557453
Why is it that all instruments seeking intelligent
life in the universe are pointed away from Earth?

[toc] | [prev] | [next] | [standalone]


#106464

FromMRAB <python@mrabarnett.plus.com>
Date2016-04-04 21:02 +0100
Message-ID<mailman.33.1459800193.32530.python-list@python.org>
In reply to#106461
On 2016-04-04 20:42, Wildman via Python-list wrote:
> I am working on a Linux gui program where I want to be able
> to click a Help button and open a man page using a viewer.
> I wrote a search function that can be called several times,
> if needed, with different arguments.  I wrote a test program
> that tries to open the Bash man page in a terminal and will
> display a message box if the search fails.  It passes the
> system path, terminal emulators and command line arguments
> to the search function.  I appears that you can't pass a list
> to a function so I am passing the arguments as strings and then
> converting them to lists for parsing in the search function.
>
> When I run the test program, I get this error:
>
> Traceback (most recent call last):
>    File "./man.py", line 38, in <module>
>      launch_help()
>    File "./man.py", line 10, in launch_help
>      if search(pathlist, executelist, commandlist):
>    File "./man.py", line 27, in search
>      subprocess.Popen(command)
>    File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
>      errread, errwrite)
>    File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
>      raise child_exception
> TypeError: execv() arg 2 must contain only strings
>
> I have not been able to figure out why I'm getting the error.
> Any help would be appreciated.  Below is the complete code for
> the test program:
>
> #!/usr/bin/env python
>
> import os, subprocess, tkMessageBox
>
> def launch_help():
>      pathlist = os.environ["PATH"]
>      executelist = "xvt,xfce4-terminal"
>      commandlist = "-e,man bash"
>      if search(pathlist, executelist, commandlist):
>          return None
>      message = "Open a terminal and enter:  man bash"
>      tkMessageBox.showinfo("Help", message)
>
> def search(pathlist, executelist, commandlist):
>      pathlist = pathlist.split(":")
>      executelist = executelist.split(",")
>      commandlist = commandlist.split(",")
>      done = False
>      for path in pathlist:
>          for execute in executelist:
>              target = path + "/" + execute
>              if os.path.isfile(target):
>                  done = True
>                  command = [target, commandlist]
>                  subprocess.Popen(command)
>              if done:
>                  break
>          if done:
>              break
>      if done:
>          return True
>      else:
>          return False
>
>
> launch_help()
>
.Popen will accept either a string or a list of strings.

You're giving it a list that contains a string and a list.

BTW, I don't know what you mean by "you can't pass a list to a 
function", because you can. How were you trying to do it?

[toc] | [prev] | [next] | [standalone]


#106480

FromWildman <best_lay@yahoo.com>
Date2016-04-04 19:38 -0500
Message-ID<VKGdnZ3OwMyClJ7KnZ2dnUU7-WEAAAAA@giganews.com>
In reply to#106464
On Mon, 04 Apr 2016 21:02:53 +0100, MRAB wrote:

> On 2016-04-04 20:42, Wildman via Python-list wrote:

<snip>

>> launch_help()
>>
> .Popen will accept either a string or a list of strings.
> 
> You're giving it a list that contains a string and a list.

Yep, that was my foolish mistake.  Thanks.

> BTW, I don't know what you mean by "you can't pass a list to a 
> function", because you can. How were you trying to do it?

The above mistake I made caused me to get a similar error
when I tried to pass the arguments as lists.  I made an
assumption that I should have not made.  After making
the correction Mr. Kelly suggested, I was able to rewrite
the code that way I had it (with the correction), passing
the arguments as lists and it works correctly.

The learning process continues... Thanks again.

-- 
<Wildman> GNU/Linux user #557453
"Philosophy is common sense with big words."
  -James Madison

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web