0

i am currently writing a program in python and i am stuck. So my questtion is: I have a program that reads a file and prints some lines to stdout like this:

#imports
import sys

#number of args
numArgs = len(sys.argv)

#ERROR if not enough args were committed
if numArgs <= 1:
    sys.exit("Not enough arguments!")

#naming input file from args
Input = sys.argv[1]

#opening files
try:
    fastQ = open(Input , 'r')
except IOError, e:
    sys.exit(e)

#parsing through file
while 1:

    #saving the lines
    firstL = fastQ.readline()
    secondL = fastQ.readline()

    #you could maybe skip these  lines to save ram
    fastQ.readline()
    fastQ.readline()

    #make sure that there are no blank lines in the file
    if firstL == "" or  secondL == "":
        break

    #edit the Header to begin with '>'
    firstL = '>' + firstL.replace('@' , '')

    sys.stdout.write(firstL)
    sys.stdout.write(secondL)

#close both files
fastQ.close()

Now i want to rewrite this program so that i can run a command line like : zcat "textfile" | python "myprogram" > "otherfile". So i looked around and found subprocess, but can't seem to figure out how to do it. thanks for your help

EDIT:

Now, if what you are doing is trying to write a Python script to orchestrate the execution of both zcat and myprogram, THEN you may need subprocess. – rchang

The intend is to have the "textfile" and the program on a cluster, so i dont need to copy any files from the cluster. i just want to login on the cluster and use the command:zcat "textfile" | python "myprogram" > "otherfile", so that the zcat and the program do their thing and i end up with "otherfile" on the cluster. hope you understand what i want to do.

Edit #2:

my solution

#imports
import sys
import fileinput

# Counter, maybe there is a better way
count = 0

# Iterieration over Input
for line in fileinput.input():

    # Selection of Header
    if count == 0 :

        #Format the Header
        newL = '>' + line.replace('@' , '')

        # Print the Header without newline
        sys.stdout.write(newL)

    # Selection of Sequence
    elif count == 1 :

        # Print the Sequence
        sys.stdout.write(line)

    # Up's the Counter
    count += 1
    count = count % 4

THX

user207421
  • 305,947
  • 44
  • 307
  • 483
  • possible duplicate of [How do you read from stdin in Python?](http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python) & [How to recognize whether a script is running on a tty?](http://stackoverflow.com/questions/858623/how-to-recognize-whether-a-script-is-running-on-a-tty) – anishsane Jan 09 '15 at 14:24
  • Please clarify your intent. Assuming what you're trying to do is write `myprogram`, you shouldn't need to use the `subprocess` module. In the use case you are describing, `myprogram` can just read its input from `sys.stdin` - no `subprocess` required. Executing `zcat "textfile" | python "myprogram"` from the OS will automatically direct `zcat`'s output to `myprogram`'s STDIN. Now, if what you are doing is trying to write a Python script to orchestrate the execution of both `zcat` and `myprogram`, THEN you may need `subprocess`. – rchang Jan 09 '15 at 14:46
  • 1
    don't put your working solution into the question, post it as an answer instead. – jfs Jan 12 '15 at 10:23

1 Answers1

0

You could use fastQ = sys.stdin to read the input from stdin instead of the file or (more generally) fastQ = fileinput.input() to read from stdin and/or files specified on the command-line.

There is also fileinput.hook_compressed so that you don't need zcat and read the compressed file directly instead:

$ myprogram textfile >otherfile
jfs
  • 399,953
  • 195
  • 994
  • 1,670