943,692 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2843
  • Python RSS
Aug 18th, 2008
0

New to Python, help printing output of system calls

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
slendergreen is offline Offline
1 posts
since Aug 2008
Aug 18th, 2008
0

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

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.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
Aug 18th, 2008
0

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

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
python Syntax (Toggle Plain Text)
  1. child = Popen(...)
  2. for line in child.stdout:
  3. print line
Reputation Points: 930
Solved Threads: 667
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Opening pipelines with python ?
Next Thread in Python Forum Timeline: Need Help With a Function....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC