如何在Python中将列表变量传递给subprocess.call命令

时间:2019-03-05 09:23:11

标签: python linux shell

我有一个清单

<!DOCTYPE html>
<html>
    <head>
        <title>NavigationBar</title>
        <link rel="stylesheet" href="newnavbar.css" type="text/css">
    </head>
  <body>

    <ul class ="first-level">
        <li><a href="#">HOME</a></li>
   	    <li><a href="#">LAB 1-5</a>
            <ul class ="second-level">
                <li><a href ="#">LAB 1</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 2</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 4</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 5</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
            </ul>
        </li>
	      <li><a href="#">LAB 6-8</a>
            <ul class ="second-level">
                <li><a href ="#">LAB 6</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 7</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 8</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
            </ul>
        <li><a href="#">LAB 9-11</a>
            <ul class ="second-level">
                <li><a href ="#">LAB 9</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 10</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 11</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
            </ul>
        <li><a href="#">LAB 12-14</a>
            <ul class ="second-level">
                <li><a href ="#">LAB 12</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 13</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
                <li><a href ="#">LAB 14</a>
                    <ul class ="third-level">
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                        <li><a href ="#">Content</a></li>
                    </ul>
                </li>
            </ul>
    </ul>

        <h2>CSC 212 - Spring 2019.</h2>
        <p>This site represents work that I have done in support of my
           efforts to learn to program in the CS1 course at SUNY Oswego
           during the Spirng 2019 semester. The materials will be temporally
           organized for the most part. Links to external sites that pertain
           to computer programming, problem solving, Java, Web development,
           and related matters will be placed after the personally created
           artifacts.</p>

  </body>
</html>

我想将此列表变量传递给linux命令,诸如此类。

apps = []
apps.append("wq35a5huqlja45jsyukrpmwuiayovrmh")
apps.append("q7mimvgduueernwvw4y22t5huemykntw")
apps.append("pmudbpyquna2bll53pwqh7gdejxtmchq")

但是我遇到以下错误。

  

TypeError:execv()arg 2必须仅包含字符串

我也尝试过此操作,但同样无法正常工作。

subprocess.call(["service", "delete", apps])

有人可以让我知道如何将变量(列表,字典等)传递给subprocess.call命令

3 个答案:

答案 0 :(得分:1)

<your-dessert>-release

将扩展为

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    char *buf = NULL;
    int size;
    unsigned int length;
    size = getline(&buf, &length, stdin);
    if (size != -1)
        /* do anything you want with the stuff that was entered here */
        /* for the example I just write it back to stdout! */
        puts(buf);
    else
        /* this would be your "end" condition */
        printf("Nothing read!\n");

    printf("Size: %d\n Length: %d\n", size, length);
    free(buf);
    return 0;
}

答案 1 :(得分:0)

  

subprocess.call(“服务删除$ apps”,shell = True)

这是错误的,因为apps不是shell变量。可以直接使用python字符串变量。

In[12]: daemon = 'ntp'
In[13]: sb.call(["service", daemon, "status"])
● ntp.service - LSB: Start NTP daemon
   Loaded: loaded (/etc/init.d/ntp; bad; vendor preset: enabled)
   Active: active (running) since Mon 2019-03-04 01:37:43 IST; 1 day 13h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2045 ExecStart=/etc/init.d/ntp start (code=exited, status=0/SUCCESS)
    Tasks: 1
   Memory: 2.1M
      CPU: 6.816s
   CGroup: /system.slice/ntp.service
           └─2345 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 123:130

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
Out[13]: 0

无论如何传递除字符串之外的任何内容,您都必须手动进行某种类型转换,因此对于列表,您将必须执行类似的操作。

>>> a = ['a', 'b', 'c']
>>> sb.call(["echo", ','.join(a)])
a,b,c
0

答案 2 :(得分:0)

我赞成Harly H的回答,但出于完整性考虑,这是另一种方式。

subprocess.run(['xargs', '-0', 'service', 'delete'],
    input='\0'.join(apps)),
    check=True,   # probably a good idea, but not strictly required
    universal_newlines=True)

这需要一个足够新的Python以拥有subprocess.run(),尽管即使使用Python 2 subprocess接口,类似的事情也是容易的,或者至少是可能的。在3.7+中,universal_newlines被更恰当地标记为text

如果xargs是一个非常长的字符串,

apps还将避免出现任何“参数列表太长”的错误。 (Google用于ARG_MAX。)

相关问题