简体   繁体   English

将perl脚本应用于目录中的每个文件,并使用Python获取输出

[英]Applying a perl script to every file in a directory and obtain output using Python

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. 我正在尝试制作一个python脚本,该脚本将打开一个目录,将perl脚本应用于该目录中的每个文件,并将其输出到多个文本文件或一个文件中。

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. 我不断发现* .pdb。 I am very new to all of this so any help trying to complete this script would help. 我对所有这些都是新手,因此尝试完成此脚本的任何帮助都会有所帮助。

*.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. 找不到* .pdb的意思是-您正在运行脚本的任何目录中都没有* .pdb ...并且在我阅读代码时-我看不到任何暗示它在'框架'内的内容当它运行perl脚本时。

you probably need os.chdir(path) before the Popen. 您可能需要在Popen之前使用os.chdir(path)。 How do I "cd" in Python? 如何在Python中“ cd”?

...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: ...使用python脚本运行对perl的可疑syscall可能会冒犯某些人,但每个人都这样做了..除此之外,我还指出:

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). 始终指定完整路径(如果您稍后要说,想从cron或没有PATH的环境中自动运行作业,这将成为一个问题)。 ie 'which perl' - put that full path in. 即“哪个perl”-输入完整路径。

./ .pdb would be better but not as good as the fullpath/ .pdb (which you could use instead of the os.chdir option). ./ .pdb会更好,但不如fullpath / .pdb(您可以使用它代替os.chdir选项)更好。

        subprocess.Popen(arg, stdout=True)

does not expand filename wildcards. 不扩展文件名通配符。 To handle your *.pdb , use shell=True . 要处理*.pdb ,请使用shell=True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM