V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
okletswin
V2EX  ›  Python

python 生成 shell 命令求教

  •  
  •   okletswin · 2015-10-15 17:49:12 +08:00 · 3376 次点击
    这是一个创建于 3108 天前的主题,其中的信息可能已经有所发展或是发生改变。
    举例, 将列表中的元素组装成一条 shell 命令
    ['awk', '{print $1}', 'file']

    要输出 awk '{print $1}' file

    有没有专门的模块来做这类事的?
    第 1 条附言  ·  2015-10-16 16:02:20 +08:00
    目前知道有模块能正确分隔字符串
    >>> import shlex
    >>> s = "awk '{print $1}' file"
    >>> s.split(' ')
    ['awk', "'{print", "$1}'", 'file'] # 错误
    >>> shlex.split(s)
    ['awk', '{print $1}', 'file'] 正确

    所以相反的, 需要另一个模块能将字符串列表正确拼回去
    >>> import xxx
    >>> l = ['awk', '{print $1}', 'file']
    >>> ' '.join(l)
    awk {print $1} file # 错误
    >>> xxx.join(l)
    awk '{print $1}' file # 正确

    so, 寻找这个 xxx 是谁
    12 条回复    2015-10-20 09:23:37 +08:00
    cdxem713
        1
    cdxem713  
       2015-10-15 18:12:18 +08:00 via iPhone
    ' '.join(['awk', '{print $1}', 'file'])
    是这意思么?
    okletswin
        2
    okletswin  
    OP
       2015-10-15 18:38:53 +08:00
    @cdxem713 这样出来的结果是
    awk {print $1} file
    xvid73
        3
    xvid73  
       2015-10-15 18:47:36 +08:00
    >>> ' '.join(['awk', "'{print $1}'", 'file'])
    "awk '{print $1}' file"

    是这个意思么?运行命令的话有 subprocess 模块。
    paw
        4
    paw  
       2015-10-15 18:49:31 +08:00
    py 里面 ' 和 " 都可以括字符串的 " ' " ' " ' 都行。。
    ' \' ' 转意下 也可以的
    xufang
        5
    xufang  
       2015-10-15 18:50:08 +08:00 via Android
    第三方 sh 模块
    cdxem713
        6
    cdxem713  
       2015-10-15 19:19:13 +08:00 via iPhone
    e
    @okletswin 对不起,没看清楚,就如三楼写的那样
    rundis
        7
    rundis  
       2015-10-16 08:52:49 +08:00 via iPhone
    import os
    cmd = ' '.join(aList)
    os.system(cmd)
    youkeshen
        8
    youkeshen  
       2015-10-16 09:55:46 +08:00
    试试 Plumbum
    okletswin
        9
    okletswin  
    OP
       2015-10-16 15:42:07 +08:00
    @xvid73 要有办法将字符串 {print $1} 转化成 '{print $1}',字符串 awk 仍然是 awk
    aec4d
        10
    aec4d  
       2015-10-16 18:25:18 +08:00
    os.system(' '.join(['awk', "'{print $1}'", 'file'])) 应该可以
    subprocess.Popen 有时会有 bug 遇到过
    aec4d
        11
    aec4d  
       2015-10-19 15:01:17 +08:00   ❤️ 1
    " ".join(pipes.quote(s) for s in strings)
    okletswin
        12
    okletswin  
    OP
       2015-10-20 09:23:37 +08:00
    @aec4d 正解! 非常感谢!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1366 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:44 · PVG 07:44 · LAX 16:44 · JFK 19:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.