Problem with redirection, buffering and chaining scripts.

Andre van Straaten andre at UltraShell.Net
Thu Jun 29 06:17:48 EDT 2000


I never called a Python script from another Python script, but probably
it might be helpful to flush the buffer with file.close().
This is even necessary if you read and write to the same file within the
same script.
The buffers are flushed always when the script terminates, but .close()
is sometimes necessary explicitely.

-avs

Thaddeus L. Olczyk <olczyk at interaccess.com> wrote:
> 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)

> ---------------------------------------------------------------------

-- 

Andre van Straaten
http://www.vanstraatensoft.com
______________________________________________
flames please to /dev/null at vanstraatensoft.com




More information about the Python-list mailing list