English 中文(简体)
某些档案中储存的互动式喷口产出
原标题:Interactive Python script output stored in some file

我如何记录由灰色字母和从中呼吁的所有文字进行的所有活动?

我有几本巴什文字,但现在写了一部叫所有这些巴什文字的灰色文字。 我想从这些文字中产生的所有产出都储存在一些档案中。

文字是互动性的“灰色”文字,即含有原始-投入线,因此,我只字不提文字。 py ́ teelog.txt for the générale script, 因为出于某些原因,没有在屏幕上看到问题。

这里是一份由她本人提名的候选人名单。

    cmd = "somescript.sh"
    try:
    retvalue = subprocess.check_call(cmd, shell=True)
except subprocess.CalledProcessError:
    print ("script command has been failed")
    sys.exit("exit from script")

你认为可以做些什么?

<><>Edit>/strong>

基于Alex的答复的两点:

  1. 如何回答产出档案中储存的问题? 例如,在<条码>ok = 原材料(prompt)上,用户将被要求提出问题,我也希望回答问题。

  2. 我读过关于开放的节目,并交流,并 did照使用,因为它将数据储存起来。 在此,产出数额很大,我还需要注意标准产出的精准。 你们是否知道这是否能够处理开放和交流方法?

最佳回答

Python /代码

>>> import sys
>>> class tee(object):
...   def __init__(self, fn= /tmp/foo.txt ):
...     self.o = sys.stdout
...     self.f = open(fn,  w )
...   def write(self, s):
...     self.o.write(s)
...     self.f.write(s)
... 
>>> sys.stdout = tee()
>>> print( hello world! )
hello world!
>>> 
$ cat /tmp/foo.txt
hello world!

这应在Adhur 2和3号楼工作。

同样,引导次区的产出,不使用

retvalue = subprocess.check_call(cmd, shell=True)

允许<代码>cmd 输出到其正常的“标准产出”,但还是灰 gr和重新排放,具体如下:

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
so, se = p.communicate()
print(so)
retvalue = p.returncode

假设你不关心标准操作员(仅为标准产出)和<代码>cmd的产出数量相当小(因为<代码>)。 如果这两种假设都与你完全想要的东西相对应,那么这种数据就很容易 t。

<>Edit:现在,在对这一答复的长篇评论中,欧佩组织已经澄清了这些细节:

  • How to make the answers on the questions stored in the output file as well? For example on line ok = raw_input(prompt) the user will be asked for the question and I would like to the answer logged as well.

职能如下:

def echoed_input(prompt):
    response = raw_input(prompt)
    sys.stdout.f.write(response)
    return response

您的申请代码(当然,这是为了与上面所示的<代码>tee第一类合作而专门写成的)<代码>raw_input。

  • I read about Popen and communicate and didn t use since it buffers the data in memory. Here amount of output is big and I need to care about standard-error with standard-output as well. Do you know if this is possible to handle with Popen and communicate method as well?

don t获得的(和标准-error)产出超过记忆的舒适程度,即几兆字比照您重新使用的机器。

如果这一假设得到满足,仅将上述假设重新编号为:

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, 
                     stderr=subprocess.STDOUT)
so, se = p.communicate()
print(so)
retvalue = p.returncode

i.e. 简单地将次游动物移至 st中。

如果您的指定官员不得不对你来的iga(或不管怎么说)感到担忧,那么

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, 
                     stderr=subprocess.STDOUT)
for line in p.stdout:
  sys.stdout.write(p)
p.wait()
retvalue = p.returncode

(在某一时间有一行并穿过一行)可能比较可取(这取决于<条码>cmd<>> 代码>,但并不期望其标准input的任何内容,当然是因为如果该标准s<>> 它期望任何事情,不会得到,问题开始变得具有挑战性。

问题回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...