Fwd: On saving data when programm is running

23 views
Skip to first unread message

Zhibin Liang

unread,
Nov 15, 2012, 4:56:01 AM11/15/12
to Sage-nt, sage-support
Dear all,

I try to send this email to sage-support but I don't have the right to do that. Hence I send to the group of number theory. I am sorry to interrupt you.

---------- Forwarded message ----------
From: Zhibin Liang <lian...@gmail.com>
Date: 2012/11/15
Subject: On saving data when programm is running
To: sage-support <sage-s...@googlegroups.com>


Dear All,

I have a basic problem on running sage.

When I am running a programme on background, I want to output some data before the end of programme in one file(ex, a txt file).

For example, I saved the following code as file A

########################
p=3

while true:
    p=p.next_prime()
################################


It will never stop but I want to see which all p it come at present.

If we write
sage<file A>file B&;

Then there is nothing on file B unitil the end of the programme(but  time is infinity)

I also tried


########################
p=3

outfile=file B
while true:
    p=p.next_prime()
   outfile.write(str(p))
################################

But the new p will cover the old p in file B.

Can I write some output command to realise my desire?

Very best wishes
ZHibin Liang




--

Very best wishes
Zhibin Liang






Building 104 Room 103
Nanhudongyuanyiqu
Wangjing, Chaoyang District
Beijing China,
100102



--

Very best wishes
Zhibin Liang






Building 104 Room 103
Nanhudongyuanyiqu
Wangjing, Chaoyang District
Beijing China,
100102

David Loeffler

unread,
Nov 15, 2012, 5:27:55 AM11/15/12
to sag...@googlegroups.com
Dear Liang,

Here are two possibilities.

- Redirect the standard output to a file, as you did with "sage<file
A>file B&;", but put the command "sys.stdout.flush()" at intervals in
your program. This will make sure that data in your program's output
buffer actually gets saved.

- When using a file object, open it with the command

outfile = open(<file name>, "a")

The argument "a" is a "mode" command -- it says that data written to
the file should be appended to what's already there, not overwrite it.
When you've written to the file, call "outfile.close()" to make sure
the buffer is flushed properly.

PS: Your message had three copies of your signature on it :-)

Regards, David
> --
> You received this message because you are subscribed to the Google Groups
> "sage-nt" group.
> To post to this group, send an email to sag...@googlegroups.com.
> To unsubscribe from this group, send email to
> sage-nt+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sage-nt?hl=en-GB.

John Cremona

unread,
Nov 15, 2012, 6:08:20 AM11/15/12
to sage-nt
Zhibin,

Sage (i.e. python) is buffering the output. If you insert the line

sys.stdout.flush()

in your code it will write out all data currently in the buffer at
that point. You may need to add "import sys" at the top of your file.

John

John Cremona

unread,
Nov 15, 2012, 6:09:32 AM11/15/12
to sage-nt
PS For some reason I did not see David's reply until after making my own!

P Purkayastha

unread,
Nov 15, 2012, 6:46:01 AM11/15/12
to sag...@googlegroups.com, sage-s...@googlegroups.com
I presume you are writing the program, say prog.sage, and running it as

$ sage prog.sage

Am I correct? If so, then you can write your program like this. You can
force printing the output by using sys.stdout.flush():

import sys
p = 3
while true:
p = p.next_prime()
print p
sys.stdout.flush() % force printing of output.

Then you can run the program in two ways:

Method 1:
---------
$ sage prog.sage < infile.txt | tee output_file.txt


Method 2:
---------
If you are running it in the background like this:

$ sage prog.sage &

then instead run it as

$ sage prog.sage <infile.txt > output_file.txt 2>&1 &


The Method 2 is not very convenient though. So, my suggestion would be
to run as in Method 1, but run it inside screen.

http://en.wikipedia.org/wiki/GNU_Screen
http://aperiodic.net/screen/quick_reference

Zhibin Liang

unread,
Nov 15, 2012, 7:23:52 AM11/15/12
to sag...@googlegroups.com, sage-s...@googlegroups.com
Dear David,John, Purkayastha,

I tested all you said and understand very well now
. All the suggestions work!


Thank you very much.


Very best wishes
Zhibin Liang

2012/11/15 P Purkayastha <ppu...@gmail.com>

For more options, visit this group at
http://groups.google.com/group/sage-nt?hl=en-GB.
--
You received this message because you are subscribed to the Google Groups "sage-nt" group.
To post to this group, send an email to sag...@googlegroups.com.
To unsubscribe from this group, send email to sage-nt+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/sage-nt?hl=en-GB.

Reply all
Reply to author
Forward
0 new messages