print to console without a line break

Q

Qian Xu

Hello All,

Is it possible to print something to console without a line break?

I tried:
sys.stdout.write("Testing something ...") // nothing will be printed
time.sleep(1)
sys.stdout.write("done\n") // now, the whole string will be printed

What I want, is to see "Testing something ..." first.
And after 1 second, to see "done" (with a line break)

The only one solution I have found is to call "echo -n 'my_string'".
But it is not nice. Because I have to escape all special chars in the string
manually.

Any advice?
Best regards
-- Qian Xu
 
Q

Qian Xu

Qian said:
Hello All,

Is it possible to print something to console without a line break?

I tried:
sys.stdout.write("Testing something ...") // nothing will be printed
time.sleep(1)
sys.stdout.write("done\n") // now, the whole string will be printed

What I want, is to see "Testing something ..." first.
And after 1 second, to see "done" (with a line break)

I have solved the problem by calling
sys.stdout.flush()

--Qian Xu
 
L

Lie Ryan

Hello All,

Is it possible to print something to console without a line break?

I tried:
sys.stdout.write("Testing something ...") // nothing will be printed
time.sleep(1)
sys.stdout.write("done\n") // now, the whole string will be printed

What I want, is to see "Testing something ..." first. And after 1
second, to see "done" (with a line break)

The only one solution I have found is to call "echo -n 'my_string'". But
it is not nice. Because I have to escape all special chars in the string
manually.

Any advice?
Best regards
-- Qian Xu

If you don't mind an extra space, you can use this:

print 'Testing something ...',
# note the trailing comma
 
A

Albert Hopkins

Probably because your stdout is line-buffered. Try:

sys.stdout.write("Testing something...")
sys.stout.flush() # flush the stdout buffer
 
Q

Qian Xu

Albert said:
On Tue, 2008-12-23 at 13:18 +0000, Lie Ryan wrote:

Probably because your stdout is line-buffered. Try:

sys.stdout.write("Testing something...")
sys.stout.flush() # flush the stdout buffer

Thanks. This works for me ^^)
 
Q

Qian Xu

Lie Ryan wrote:
If you don't mind an extra space, you can use this:

print 'Testing something ...',
# note the trailing comma

Thanks. However, the contents will be saved in buffer and will not be shown
until print "(last piece)"

sys.stdout.flush() can solve this problem :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top