Problem with redirection, buffering and chaining scripts.

Thaddeus L. Olczyk olczyk at interaccess.com
Wed Jun 28 14:01:36 EDT 2000


I'm having this problem chaining two scripts together 
( Windows NT ), demonstrated by the demo below.

When script1.py calls script2.py nothing gets printed
until the script finishes. Then the whole output of script2.py
is flushed. Taking one of the command and executing it by hand,
the output comes one line at a time ( as I want it ).

Any idea of how to get script1.py to print output from
script2.py as it comes?

----------------------script1.py--------------------------------
import os,stat,time

def scan_dir(path,func):
    filenames=os.listdir(path)
    for filename in filenames:
        st=os.stat(path+'/'+filename)
        if stat.S_ISDIR(st[0]):
            scan_dir(path+'/'+filename,func)
            func(path+'/'+filename)       

def print_dir(x):
    command="e:/python/python -u script2.py "+x
    print command
    for line in os.popen(command+' 2>&1','r').readlines():
        print ">>",line,

scan_dir("l:/",print_dir)
------------------script2.py--------------------------------------

import os,sys,time


fname=sys.argv[1]

command="e:/bin/ls -l "
command=command+fname+' 2>&1'
print command
for line in os.popen(command,'r').readlines():
   print ">>",line,
   time.sleep(1)

---------------------------------------------------------------------



More information about the Python-list mailing list