更好地解决方案是使用一个真正的解析器,如shlex模块提供的解析器。以下是一个简单的例子,它使用shlex类打印输入文件中找到的token。 #! /usr/bin/python import shlex import sys if len(sys.argv) ! [1] body = file(filename, 'rt').read() print 'ORIGINAL:', repr(body) print print 'TOKENS:' lexer = shlex.shlex 用shlex完全可以找出包含嵌入式撇号的token 执行 python shlex_example.py apostrophe.txt 结果: ORIGINAL: "This string has 可以看出shlex非常智能,比正则表达式方便多了。
rsyslog.conf', '/etc/sensors3.conf', '/etc/GeoIP.conf', '/etc/Trolltech.conf', '/etc/nfsmount.conf', shlex kthreadd] 3 2 [migration/0] 4 2 [ksoftirqd/0] [root@133 ~]# ipython In [1]: import shlex In [3]: shlex.split In [5]: subprocess.check_call(shlex.split
子进程通过Process类来设置,示例如下 from multiprocessing import Process import subprocess import shlex def cal_seqs sequences : {}'.format(cnt / 4)) def fastqc(fq): cmd = 'fastqc -t 10 {}'.format(fq) cmd_args = shlex.split 主进程再直接执行,也就是阻塞式的运行,需要join函数来进行阻塞,上述代码修改如下 from multiprocessing import Process import subprocess import shlex sequences : {}'.format(cnt / 4)) def fastqc(fq): cmd = 'fastqc -t 10 {}'.format(fq) cmd_args = shlex.split p.join() 上述代码还可以用map方法进行改写,更加简便,改写之后的完整代码如下 from multiprocessing import Pool import subprocess import shlex
(s) subf.stdin.close() print subf.stdou.read() 1.3 import glob glob.glob(‘a*’) —> 返回以a开头的文件名 1.4 shlex —> 切割模块 import shlex mysql_conn = ‘mysql -u root -p 123456 -m “hello world”’ args = shlex.split(mysql_conn ) args subf =subprocess.Popen(shlex.split(mysql_conn),stdout=subprocess.PIPE,stdout=subprocess.PIPE,shell
0): import urllib else: from urllib.request import urlopen import json import subprocess, shlex thisProjectPath) if os.path.exists(thisProjectPath): command = shlex.split ('git -C "%s" pull' % (thisProjectPath)) else: command = shlex.split(
import shlex import datetime import subprocess import time def DoShell(cmd, cwd=None, timeout=2, shell=False): if shell: cmd_string_list = cmd else: cmd_string_list = shlex.split
2 split字符串时忽略引号中的空格 import shlex my_str = 'a b c "d e f" g' print shlex.split(my_str) # ['a', 'b', ' c', 'd e f', 'g'] print my_str.split() # ['a', 'b', 'c', '"d', 'e', 'f"', 'g'] 示例中,通过shlex.split函数,在
2 split字符串时忽略引号中的空格 import shlex my_str = 'a b c "d e f" g' print shlex.split(my_str) # ['a', 'b', ' c', 'd e f', 'g'] print my_str.split() # ['a', 'b', 'c', '"d', 'e', 'f"', 'g'] 示例中,通过shlex.split函数,在
/usr/bin/env python import shlex from subprocess import Popen,PIPE def get_ps(): cmd = 'ps ax -o pid ,ppid,cmd' p = Popen(shlex.split(cmd),stdout=PIPE) return p.stdout.readlines()[1:] def split(s):
Python import subprocess import shlex def run_command(command): process = subprocess.Popen(shlex.split
直接将软件的设置一个个分隔成列表元素是比较繁琐的,推荐下列写法 >>> import shlex, subprocess>>> command_line = 'ls -l'>>> args = shlex.split (command_line)>>> subprocess.run(args) 通过shlex模块的split功能,可以将字符串分隔成合适的参数列表。
他在脚本中使用以下代码调用Java程序:x = 'java -cp "lib/*:esalib.jar" clldsystem.esa.ESAAnalyzer %s %s' % (word1, word2)args = shlex.split /clldsystem.esa.ESAAnalyzer %s %s' % (word1, word2)args = shlex.split(x)print argsp = subprocess.Popen = "word2"x = 'java -cp "lib/*:esalib.jar" clldsystem.esa.ESAAnalyzer %s %s' % (word1, word2)args = shlex.split
./1.py', 2.glob.iglob 相比glob.glob,获取一个可遍历对象,使用它可以逐个获取匹配的文件路径名 三、shlex模块 shlex.split(s[, comments[, posix]]) 使用类似shell的语法分割字符串s,默认按空格分隔,并且shlex.split()能识别引号,认为引号里的为一个元素。 例: In [7]: import shlex In [8]: shlex.split('hello","word') Out[8]: ['hello,word']
/usr/bin/python import lldb import commands import optparse import shlex def ls(debugger, command, command 是一个字符串,是我们命令的参数,通过情况下,我们可以使用 shlex 模块的 shlex.split(command) 命令切割处理。当然,本例直接透传就可以了。 /usr/bin/python import lldb import commands import optparse import shlex import re def ls(debugger
/usr/bin/env python #coding:utf-8 import json import shlex from subprocess import * def server_info (ip): cmd = "/usr/bin/ansible {ip} -m setup".format(ip=ip) raw_info = subprocess.check_output(shlex.split
layne_command.py),代码如下: #coding=utf-8 #自定义lldb命令 import lldb import commands import optparse import shlex debugger.GetSelectedTarget() process = target.GetProcess() thread = process.GetSelectedThread() command_args = shlex.split ④optparse和shlex是用于解析参数的两个重要的库。通过optparse来生成解析器。
/usr/bin/python import lldb import commands import optparse import shlex def ls(debugger, command, command 是一个字符串,是我们命令的参数,通过情况下,我们可以使用 shlex 模块的 shlex.split(command) 命令切割处理。当然,本例直接透传就可以了。 /usr/bin/python import lldb import commands import optparse import shlex import re def ls(debugger
, type: {} value: {}".format( type(option2), option2)) # do stuff if __name__ == '__main__': import shlex cli(shlex.split( '''--option1 '["o11", "o12", "o13"]' --option2 '["o21", "o22", "o23"]' ''')) 检测结果:
解决方法 建立脚本 采用以下脚本生成ignore_specified_objc_exceptions.py文件 import lldb import re import shlex # This script : # We can't handle anything except objc_exception_throw return None filters = shlex.split
1.函数如何被调用,通过return返回值来调用 2.生成器和return区别 yield 生成器返回对象,可以迭代 可以执行 glob模块 类似shell中的正则匹配 shlex模块 Popen 将命令参数直接分词 cmd = "ps ax -o pid,ppid,cmd" shlex.split(cmd) ['ps','ax','-o','pid,ppid,cmd'] ['mysql','-u','root', '-p123','-e','show processlist'] p = Popen(shlex.split(cmd),stdout=PIPE) p.communicate() ('',None) 代替