python编译成可执行文件 运行后在控制台输出结果_如何在程序完成(Python)后将控制台打印到文本文件?...

I have a program that outputs many calculations and results to the console through the print statement. I want to write some code to export (or save) all the contents of the console to a simple text file.

I searched StackOverflow and other sites but I found some methods to redirect the print statement to print to a file directly, but I want the program to work normally, to display outputs to the console, then to save its contents AFTER all operations of the program done.

I am using PyCharm with Python2.7 if it matters

解决方案

Ok, so normally to get it done, you have to rewrite python print built-in function. But... There is ipython, which provides some hooks.

First you need to have ipython installed:

#bash

sudo pip install ipython

(I'm using sudo to simple locate then folder I need to reach, read further)

After ipython installation you'll have ipython extensions folder available, so get to it:

#bash

cd ~/.ipython/extensions/

and create there let's say a file called print_to_file.py, here is its content:

#python

class PrintWatcher(object):

def __init__(self, ip):

self.shell = ip

def post_execute(self):

with open('/home/turkus/shell.txt', 'a+') as f:

in_len = len(self.shell.user_ns['In'])

i = in_len - 1

in_ = self.shell.user_ns['In'][i]

out = self.shell.user_ns['Out'].get(i, '')

# you can edit this line if you want different input in shell.txt

f.write('{}\n{}\n'.format(in_, out))

def load_ipython_extension(ip):

pw = PrintWatcher(ip)

ip.events.register('post_run_cell', pw.post_execute)

After saving a file just run:

#bash

ipython profile create

# you will get something like that:

[ProfileCreate] Generating default config file: u'/home/turkus/.ipython/profile_default/ipython_config.py'

Now get back to setting up our hook. We must open ipython_config.py created under path above and put there some magic (there is a lot of stuff there, so go to the end of file):

# some commented lines here

c = get_config()

c.InteractiveShellApp.extensions = [

'print_to_file'

]

After saving it, you can run ipython and write your code. Every your input will be written in a file under path you provided above, in my case it was:

/home/turkus/shell.txt

Notes

You can avoid loading your extension every time ipython fires up, by just delete 'print_to_file' from c.InteractiveShellApp.extensions list in ipython_config.py. But remember that you can load it anytime you need, just by typing in ipython console:

➜ ~ ipython

Python 2.7.12 (default, Jul 1 2016, 15:12:24)

Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.

In [1]: %load_ext print_to_file

Any change in print_to_file.py is being reflected in open ipython shell after using %reload_ext print_to_file command, so you don't have to exit from and fire up it again.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值