Start New Discussion within our Software Development Community

I wrote a python script that checks for new media on my windows box, if it is new it copys it over to my standalone freevo jukebox. Below is a small part of the script that does the actually copying

source = "/mnt/samba/%s" %i
destination = "/mnt/media/movies"
print "copying %s to %s" %(i, destination)
shutil.copy2(source, destination)

when I ssh into the freevo box then run the script it behaves like I think it shoud. first it runs the print function, then runs the shutil.copy2 function.

here is where it is behaving buggy. if I run the script directly using a command like this

ssh root@192.168.1.103 /usr/local/bin/sambacopy.py

it does not print to the screen until after it has finished copying. I have found a solution of using sys.stdout.flush() directly after the print statment

source = "/mnt/samba/%s" %i
destination = "/mnt/media/movies"
print "copying %s to %s" %(i, destination)
sys.stdout.flush()
shutil.copy2(source, destination)

why is the flush function needed when I run the script directly, but not if I ssh into a shell , then run the script

I have seen something similar when I mixed GUI with a few test prints. Python must internally prioritize, maybe has something to do with the internal memory manager.

This question has already been answered. Start a new discussion instead.