简体   繁体   English

捕获Python脚本输出

[英]capture Python script output

I have written very small script and try to capture output of the script. 我编写了非常小的脚本,并尝试捕获脚本的输出。 I have written multiple time similar way but never had issue. 我以类似的方式写过多次,但从未遇到过问题。 Could I have input. 我可以输入吗? I think I am doing very silly mistake 我想我犯了很愚蠢的错误

numpy_temp = """
import numpy
import sys
a, b, c = numpy.polyfit(%s,%s, 2)
print a, b, c""" %(x, y)
fp_numpy = open("numpy_temp.py", "w")
fp_numpy.write(numpy_temp)
cmd = "/remote/Python-2.7.2/bin/python numpy_temp.py "
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE,
            stderr = subprocess.PIPE, shell = True)
out, err = proc.communicate()
print "out", out

You're never actually closing fp_numpy , so the script can be empty or incomplete at the time you try to run it. 您实际上从未关闭过fp_numpy ,因此在您尝试运行该脚本时,该脚本可能为空或不完整。

It isn't guaranteed to be empty, but it's very likely . 它不能保证为空,但很有可能 When I try this on two different *nix computers with 7 different versions of Python, it's empty every time… (The fact that, after your script finishes, the file gets closed, and therefore flushed, makes the problem harder to debug.) 当我在装有7种不同版本的Python的两台不同的* nix计算机上尝试此操作时,它每次都是空的(事实是,在脚本完成后,文件被关闭并因此被刷新,这使得问题更难以调试。)

The best way to fix this is to use a with statement, so it's impossible to forget to close the file: 解决此问题的最佳方法是使用with语句,因此不可能忘记关闭文件:

with open("numpy_temp.py", "w") as fp_numpy:
    fp_numpy.write(numpy_temp)

But beyond that, you've got another problem. 但是除此之外,您还有另一个问题。 If the generated script raises an exception, it will print nothing to stdout, and will dump a traceback to stderr, which you will read and ignore. 如果生成的脚本引发异常,它将不输出任何内容到stdout,并将回溯转储到stderr,您将读取并忽略它。 It's very hard to debug problems when you're ignoring the errors… I don't know what you're passing for x and y , but if you're, say, passing a numpy.array instead of a string that evaluates to one, you could easily get an exception and never see it. 当您忽略错误时,调试问题非常困难……我不知道您为xy传递了什么,但是,例如,如果要传递numpy.array而不是计算结果为一个的字符串,您很容易就能获得异常,而永远不会看到它。 Either send stderr to stdout, or print "err", err at the end. 将stderr发送到stdout,或在末尾print "err", err

And finally, you really shouldn't be using a command string and shell=True here, because you end up with an extra level of indirection for no good reason, which can also make things harder to debug. 最后,您实际上不应该在这里使用命令字符串和shell=True ,因为没有充分的理由,您最终会获得额外的间接级别,这会使调试变得更加困难。 Just do this: 只要这样做:

cmd = ["/remote/Python-2.7.2/bin/python", "numpy_temp.py"]
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE)

使用print out而不是print "out"

Check out this method 看看这个方法

see.py see.py

for line in range(6):
    print line

me@pubx:~$ python see.py > all me @ pubx:〜$ python see.py>全部

I don't know why you'd want to write a script to capture the output of a script (maybe you have a good reason). 我不知道您为什么要编写脚本来捕获脚本的输出(也许您有充分的理由)。 this sort of task is much easier using the shell. 使用shell这类任务要容易得多。

Create your numpy_temp.py and then pipe it's output to a file. 创建您的numpy_temp.py ,然后将其输出管道传输到文件。

/remote/Python-2.7.2/bin/python numpy_temp.py > output.txt

To append the data to an existing file use 要将数据附加到现有文件中,请使用

/remote/Python-2.7.2/bin/python numpy_temp.py >> output.txt

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

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