11

In some part of my python program , I need to do this -

  1. print a message on the terminal (using the print command)
  2. make some directories (using os.mkdirs)
  3. copy a file (using shutil.copy2)
  4. Again display a message on terminal (using the print command)

(All this is in a loop)

Now the problem is, although command '1.' is executed before '2.' , '3.' and '4.', it actually displays the message of command '1.' on the screen after all 4 commands have been executed, (along with the message of command '4.'). What I want is that it should display the message of command '1.' first, then start with the remaining piece of code... How can I make this happen? (Is there something like flushing for the print command as well?)

moinudin
  • 134,091
  • 45
  • 190
  • 216
Pushpak Dagade
  • 6,280
  • 7
  • 28
  • 41
  • Possible duplicate of [How to flush output of Python print?](http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print) – Łukasz Rogalski Sep 18 '16 at 22:49

1 Answers1

12

This might be what you are looking for How to flush output of Python print?

Community
  • 1
  • 1
Navi
  • 8,580
  • 4
  • 34
  • 32
  • 5
    Wow, some guy got +62 for knowing about `sys.stdout` and the `.flush` method of file-likes? –  Jan 15 '11 at 11:07