1

I've inserted some shell commands into python script like below:

#!/usr/bin/python

import os,sys,re
import gzip
import commands

path = "/home/x/nearline"

for file in os.listdir(path):
  if re.match('.*\.recal.fastq.gz', file):
    fullpath = os.path.join(path, file)
    result = commands.getoutput('zcat fullpath |wc -l')
    numseqs = int(result)/4.0
  print numseqs

zcat fullpath |wc -l is the shell command inserted.

Problem is, I define fullpath here for all fastq files, but after being put under ' ', seems this fullpath doesn't work. How can I solve this problem?

3 Answers 3

5

You have to concatenate the string with the value of the variable:

result = commands.getoutput('zcat ' + fullpath + ' |wc -l')
Sign up to request clarification or add additional context in comments.

2 Comments

Also, you're better off using for aFile in glob.glob(path+'/*.recal.fastq.gz') rather than bothering with os.listdir() and os.path.join().
Even if there weren't the option of using glob here, the normal, simple spelling of re.match('.*\.recal.fastq.gz', file) is file.endswith('.recal.fastq.gz') (assuming you meant to escape the other dots in the regex too).
3

fullpath is a variable, you need to concatenate it with the rest of command like this:

result = commands.getoutput('zcat ' + fullpath + ' |wc -l')

Comments

1

Try

commands.getoutput('zcat ' + fullpath + ' |wc -l')

since python variables aren't auto-expanded in strings.

2 Comments

This actually won't work...you omitted spaces around fullpath
True. Corrected, though I'd better delete this answer when there's 3 identical ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.