Home > Net >  Python subprocess() reading file in bash
Python subprocess() reading file in bash

Time:11-29

I have a shell command for a file as given below:

filename="/4_illumina/gt_seq/gt_seq_proccessor/200804_MN01111_0025_A000H35TCJ/fastq_files/raw_data/200804_MN01111_0025_A000H35TCJ.demultiplex.log"

assembled_reads=$(cat $filename | grep -i " Assembled reads ...................:" | grep -v "Assembled reads file...............:")

Now I am trying to run this within a python environment using subprocess as:

task = subprocess.Popen("cat $filename | grep -i " Assembled reads ...................:" | grep -v "Assembled reads file...............:"", shell=True, stdout=subprocess.PIPE)
p_stdout = task.stdout.read()
print (p_stdout)

This is not working becasue I am not able to parse the filename variable from python to shell and probably there is a syntax error in the way I have written the grep command.

Any suggestions ?

CodePudding user response:

This code seems to solve your problem with no external tools required.

filename="/4_illumina/gt_seq/gt_seq_proccessor/200804_MN01111_0025_A000H35TCJ/fastq_files/raw_data/200804_MN01111_0025_A000H35TCJ.demultiplex.log"
for line in open(filename):
    if "Assembled reads" in line and "Assembled reads file" not in line:
        print(line.rstrip())

CodePudding user response:

I would consider doing all the reading and searching in python and maybe rethink what you want to achieve, however:

In a shell:

$ export filename=/tmp/x-output.GOtV 

In a Python (note the access to $filename and mixing quotes in the command, I also use custom grep command to simplify things a bit):

import os
import subprocess
tmp = subprocess.Popen(f"cat {os.environ['filename']} | grep -i 'x'", shell=True, stdout=subprocess.PIPE)
data = tmp.stdout.read()
print(data)

Though working, the solution is ... not what I consider a clean code.

  • Related