无法使用Popen.communicate发送文件"<(echo ..."文件无法访问:`/ dev / fd / 62'

时间:2018-02-06 18:54:05

标签: python command-line subprocess

我试图通过命令行发送文件(这适用于Mac和ubuntu服务器机器)但由于某种原因,在生产服务器中我收到错误

 cmd_list = [
        'blastn',
        '-query',"<(echo -e \">{}\\n{}\")".format('seq', seq),
        '-subject',"<(echo -e \">{}\\n{}\")".format('sec_rc', seq_rc),
        '-reward','2',
        '-max_target_seqs','1',
        '-penalty','-4',
        '-word_size','7',#'-ungapped',
        '-evalue','1','-strand',"plus",
        #'-soft_masking','false' ,'-dust','no',
        '-outfmt',"'6 sstart send qstart qend score length mismatch gaps gapopen nident'"]
        cmd = " ".join(cmd_list)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True, executable='/bin/bash')
        out,err = p.communicate()

这是我得到的错误,我不知道它是什么

Command line argument error: Argument "subject". File is not accessible:  `/dev/fd/62'

这是我的制作机器

 lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial

1 个答案:

答案 0 :(得分:1)

原始代码是不必要的错误和不安全(&#34;不安全&#34;因为你有参数,否则这些数据会被连接成一个被解析为代码的字符串)。 不要在这里使用shell。相反,让Python解释器执行相同的工作,否则要求shell执行但使用明确命名的FIFO。

import subprocess, tempfile, threading, os

def writeToFifo(filename, content):
    with open(filename, 'w') as f:
        f.write(content)

redirect_cmd = ['bash', '-c', 'exec >"$1"; shift; exec "$@"', "_"]

fifo_dir = tempfile.mkdtemp()
try:
    query_fifo = os.path.join(fifo_dir, 'query_fifo')
    subject_fifo = os.path.join(fifo_dir, 'subject_fifo')
    os.mkfifo(query_fifo)
    os.mkfifo(subject_fifo)

    query_thread = threading.Thread(target=writeToFifo, args=(query_fifo, seq))
    subject_thread = threading.Thread(target=writeToFifo, args=(subject_fifo, seq))

    cmd_list = [
        'blastn',
        '-query', query_fifo,
        '-subject', subject_fifo,
        '-reward','2',
        '-max_target_seqs','1',
        '-penalty','-4',
        '-word_size','7',#'-ungapped',
        '-evalue','1','-strand',"plus",
        #'-soft_masking','false' ,'-dust','no',
        '-outfmt','6 sstart send qstart qend score length mismatch gaps gapopen nident'
    ]

    query_thread.start()
    subject_thread.start()
    p = Popen(cmd_list, stdout=PIPE, stderr=PIPE)
    out,err = p.communicate()
finally:
    shutil.rmtree(fifo_dir)