arg2 = f'cat <(grep \'#\' temp2.vcf) <(sort <(grep -v \'#\' temp2.vcf) | sortBed -i - | uniq ) > out.vcf'
print(arg2)
try:
subprocess.call(arg2,shell=True)
except Exception as error:
print(f'{error}')
當我運行它時,我收到以下錯誤:
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(grep '#' temp2.vcf) <(sort <(grep -v '#' temp2.vcf) | sortBed -i - | uniq ) > Out.vcf'
但是當我在命令列中運行時它可以作業。
uj5u.com熱心網友回復:
直接的錯誤是您的嘗試使用了Bash-specific syntax。您可以使用executable="/bin/bash"關鍵字引數解決此問題;但實際上,您為什么要在這里使用復雜的外部管道?Python可以做所有這些事情,除了sortBed本機。
with open("temp2.vcf", "r"
) as vcfin, open("out.vcf", "w") as vcfout:
sub = subprocess.Popen(
["sortBed", "-i", "-"],
text=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
for line in vcfin:
if "#" in line:
vcfout.write(line)
else:
sub.stdin.write(line)
subout, suberr = sub.communicate()
if suberr is not None:
sys.stderr.write(suberr)
seen = set()
for line in subout.split("\n"):
if line not in seen:
vcfout.write(line "\n")
seen.add(line)
Python 的重新實作稍微笨拙(并且未經測驗,因為我沒有sortBed或您的輸入資料),但這也意味著如果您想修改某些內容,在哪里更改更明顯。
uj5u.com熱心網友回復:
Python 的call()函式默認呼叫命令 with sh。行程替換語法由 支持bash,但不由sh.
$ sh -c "cat <(date)"
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cat <(date)'
$ bash -c "cat <(date)"
Mon Mar 14 11:12:48 PDT 2022
如果你真的需要使用特定于 bash 的語法,你應該能夠指定 shell 可執行檔案(但我沒有嘗試過):
subprocess.call(arg2, shell=True, executable='/bin/bash')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/445480.html
上一篇:如果列小于值替換其他列值awk
