1

I am trying to this Linux command for Perl (v5.10.1) in Python (v2.6)

perl tilt.pl *.pdb > final.txt

What do I do so that I can apply the Perl script to every PDB file, examples would be best?

my current script is this:

import shlex, subprocess
arg_str = "perl tilt.pl frames > final.txt"
arg = shlex.split(arg_str)

print(arg)

import os
framespdb = os.listdir("prac_frames")

for frames in framespdb:
        subprocess.Popen(arg, stdout=True)

2 Answers 2

3

You can use shell=True

BTW: I think you try to put variable frames in place of text frames in command so I use %s and line % frames to do this.

import os
import subprocess

line = "perl tilt.pl %s > final.txt"

framespdb = os.listdir("prac_frames")

for frames in framespdb:
    cmd = line % frames
    print(cmd)
    subprocess.Popen(cmd, shell=True)

EDIT: if you need results in different files you can use again %s to add unique filename to command - for example:

import os
import subprocess

line = "perl tilt.pl %s > final-%s.txt" # second `%s`

framespdb = os.listdir("prac_frames")

for frames in framespdb:
    cmd = line % (frames, frames) # second `frames`
    print(cmd)
    subprocess.Popen(cmd, shell=True)

EDIT: normally in shell you can send result on screen or redirect it to file > file.txt. To get text on screen and save it in file you need shell command tee and python command subprocess.check_output():

import os
import subprocess

line = "perl tilt.pl %s | tee final-%s.txt"

framespdb = os.listdir("prac_frames")


for frames in framespdb:
    cmd = line % (frames, frames)
    print(cmd)
    output = subprocess.check_output(cmd, shell=True)
    print 'output:', output
Sign up to request clarification or add additional context in comments.

3 Comments

That worked very well. How would I get the out put in a text file from this? I keep getting "Stop! File 'content from prac_frames' not not found!"
output goes to final.txt and overwrite previous result - you can use >> to get all results in final.txt. Or you can use another %s in line to save to different files. See new example in answer.
if you need result from perl script in python script then perl script has to print it on screen (it's call output in doumentation) but normal redirection > send result to file - not on screen. You need shell command tee to send perl script result on screen and into file. See new example.
1

You can call a subprocess like from the shell if you override the shell parameter:

subprocess.call(arg_str, shell=True)

Comments

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.