RSS Forums RSS

New to Python, help printing output of system calls

Please support our Python advertiser: Programming Forums
Reply
Posts: 1
Reputation: slendergreen is an unknown quantity at this point 
Solved Threads: 0
slendergreen slendergreen is offline Offline
Newbie Poster

New to Python, help printing output of system calls

  #1  
Aug 18th, 2008
I'm new to Python and not anywhere near a competent programmer.

Anyway, if someone could help me get over this hump, I'd appreciate it.

I need to write a Python script that calls commands from the OS that may have lengthy output. Let's say I want to see the results of an rsync between two directories. I'd like to print the output in real-time to a web page.

My web page calls the Python script. No problem.
My Python script does a system call to rsync. No problem.

Here's were I get puzzled. No output from the rsynch makes it to the screen. It runs, but I can't see that anything is happening until the rsync is finished.

What would be the correct technique for doing this kind of thing? Basically I want to call system functions and have their status output appear just as I would have seen it from the command line.

Thanks for any tips.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 116
Reputation: slate is an unknown quantity at this point 
Solved Threads: 26
slate slate is offline Offline
Junior Poster

Re: New to Python, help printing output of system calls

  #2  
Aug 18th, 2008
Try subprocess.Popen stdout redirection to a file or to the web server input.

However I would not expose a long running server process via web. I think it would be better to schedule the process and present the output.
Reply With Quote  
Posts: 521
Reputation: Gribouillis will become famous soon enough Gribouillis will become famous soon enough 
Solved Threads: 92
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Pro

Re: New to Python, help printing output of system calls

  #3  
Aug 18th, 2008
It would be easier if we could see the python line which runs the system call.
A possible solution to your problem is to start the system call without waiting for it's completion, using subprocess.Popen (see http://docs.python.org/lib/node533.html). Then, once you have a child process running, print it's output with a loop, like this
  1. child = Popen(...)
  2. for line in child.stdout:
  3. print line
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Python Forum
Views: 1059 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:16 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC