Simple question

Steve Holden steve at holdenweb.com
Mon Jun 5 10:02:46 EDT 2006


bigodines wrote:
> Hi guys!
> 
> I'm a complete newbie in Python and I'm trying to make a small software
> to watch my network. It will be a simple snmpget report for a specific
> machine.
> 
> I would like to make a small program in python to be runed with
> crontrab that will store the whole output in a txt file. I know its not
> a big deal, but i've no background in python :)
> 
> basically i would like to know how to:
> 
> 1 - execute a command in the server

The normal way would be using the command

     python script.py

> 2 - write the whole output into a file.
> 
Well, one way would simply be to use output redirection, such as

     python script.py > /tmp/file.txt

Another alternative is to allow Python to open a file using

     f = open("some/file/name.txt", "w")

and then use

     f.write(...)

or

     print >> f, ...

statements to send the output to the given file, finally closing it with

     f.close()

The advantage of this latter method is that you can compute the filename 
(the first argument to the open() function) in Python if you want, using 
elements like the current date and time.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list