【问题标题】:Python3 How should I execute this complex shell commandPython3我应该如何执行这个复杂的shell命令
【发布时间】:2018-10-30 15:43:11
【问题描述】:

我想通过python执行这个shell命令,我试过用

shell="ls *R1.fastq.gz|while read a; do b=${a%R1.fastq.gz}R2.fastq.gz; c=${a%R1.fastq.gz}R1.out.fq; d=${a%R1.fastq.gz}R2.out.fq; e=${a%R1.fastq.gz}.s.fq; echo "sickle pe -t sanger -f $a -r $b -o $c -p $d -s $e"; done >SICKLE.sh"

system.os(shell)

但是得到一个SythaxError: InvalidSyntax 我做了一些研究,也许我应该使用subprocces ?但我对此很陌生,我们能帮帮我吗?

【问题讨论】:

  • 另外你必须在你的命令中转义引号,或者使用单引号来包含你的字符串。
  • @chrisz 转义引号是什么意思?搭上他们?但如果没有它们,它将无法正常工作
  • 一种方式是这样的:shell="ls *R1.fastq.gz|while read a; do b=${a%R1.fastq.gz}R2.fastq.gz; c= ${a%R1.fastq.gz}R1.out.fq;d=${a%R1.fastq.gz}R2.out.fq;e=${a%R1.fastq.gz}.s.fq ; echo \"sickle pe -t sanger -f $a -r $b -o $c -p $d -s $e\"; 完成 >SICKLE.sh"

标签: python-3.x shell command subprocess os.system


【解决方案1】:

escape 引号(在字符串中的引号前添加\

shell="*R1.fastq.gz|while read a; do b=${a%R1.fastq.gz}R2.fastq.gz; c=${a%R1.fastq.gz}R1.out.fq; d=${a%R1.fastq.gz}R2.out.fq; e=${a%R1.fastq.gz}.s.fq; echo \"sickle pe -t sanger -f $a -r $b -o $c -p $d -s $e\"; done >SICKLE.sh"

subprocess.call

import subprocess
subprocess.call(["ls", shell])

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2020-11-13
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多