gpt4 book ai didi

python - Python 中用于 Perl 的 Linux 命令

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:40 26 4
gpt4 key购买 nike

我正在尝试在 Python (v2.6) 中为 Perl (v5.10.1) 使用这个 Linux 命令

perl tilt.pl *.pdb > final.txt

我该怎么做才能将 Perl 脚本应用于每个 PDB 文件,最好有示例?

我当前的脚本是这样的:

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)

最佳答案

您可以使用shell=True

顺便说一句:我认为你试图在命令中用变量 frames 代替文本 frames 所以我使用 %s line % frames 来执行此操作。

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)

编辑:如果您需要不同文件中的结果,您可以再次使用 %s 将唯一的文件名添加到命令中 - 例如:

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)

编辑: 通常在 shell 中,您可以在屏幕上发送结果或将其重定向到文件 > file.txt。要在屏幕上获取文本并将其保存在文件中,您需要 shell 命令 tee 和 python 命令 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

关于python - Python 中用于 Perl 的 Linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com