0

I am trying to make a python script that will open a directory, apply a perl script to every file in that directory and get its out put in either multiple text files or just one.

I currently have:

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

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

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

I keep getting *.pdb not found. I am very new to all of this so any help trying to complete this script would help.

2 Answers2

0

*.pdb not found means exactly that - there won't be a *.pdb in whatever directory you're running the script... and as I read the code - I don't see anything to imply it's within 'frames' when it runs the perl script.

you probably need os.chdir(path) before the Popen. How do I "cd" in Python?

...using a python script to run somewhat dubious syscalls to perl may offend some people but everyone's done it.. aside from that I'd point out:

always specify full paths (this becomes a problem if you will later say, want to run your job automatically from cron or an environment that doesn't have your PATH). i.e. 'which perl' - put that full path in.

./.pdb would be better but not as good as the fullpath/.pdb (which you could use instead of the os.chdir option).

Community
  • 1
  • 1
pacifist
  • 712
  • 4
  • 13
  • I am trying to use os.listdir as the directory to find '*.pdb' files it comes up with five "Stop! File *.pdb not not found!" and there are 5 files in there I am trying to get this to run on. I am a little confused on how to use the os.chdir(path) for this. I am very new at all of this just so you know. – Chemist not a Programmer Jul 22 '14 at 00:37
  • I think I figured something out but if you could send me an example using os.chdir(path), that would be very helpful! – Chemist not a Programmer Jul 22 '14 at 00:58
0
        subprocess.Popen(arg, stdout=True)

does not expand filename wildcards. To handle your *.pdb, use shell=True.

Armali
  • 18,255
  • 14
  • 57
  • 171