By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Help taking output from a python script and putting it into a txt file

P: 1
Hi guys,

I'm new to Python and have a ? that some of you can maby help me on. I'm using python 2.4 and running the script from a windows machine getting data from a linux machine. I have a script that returns information from the linux box to me ( cpu usage, free memory, ect). What I need help on is taking that information ( output) and put that information into a txt file. Is this a simple task? Any help will be appreciated and thanks for any and all help.

here is the source code im using to get the information

Expand|Select|Wrap|Line Numbers
  1.  
  2. import xmlrpclib
  3. import getopt
  4. import types
  5. import sys
  6. import exceptions
  7.  
  8. class _Method:
  9.     def __init__(self,send,name,*args):
  10.         self.__send = send
  11.         self.__name = name
  12.         self.__fun = getattr(send, name)
  13.         self.__args = args
  14.     def __getattr__(self, name):
  15.         return Method(self.__send, "%s.%s" % (self.__name, name))
  16.     def __call__(self, *args):
  17.         ret={}
  18.         for x in self.__args:
  19.             for y in x:
  20.                 ret[y]=x[y]
  21.         for x in args:
  22.             for y in x:
  23.                 ret[y]=x[y]
  24.         ack=self.__fun(ret)
  25.         if ack['Acknowledgement']['code'] != 200:
  26.             raise Exception('Error in ' + self.__name, ack)
  27.         return ack
  28.  
  29.  
  30.  
  31. class Connection:
  32.     def __getattr__(self, name):
  33.         return _Method(self.__xmlrpc,name,self.__auth)
  34.  
  35.     def __init__(self, *args):
  36.         optlist, optargs = getopt.getopt(args, "u:p:")
  37.  
  38.         self.username = 'administrator'
  39.         self.password = 'administrator'
  40.  
  41.         for o,a in optlist:
  42.             if o == '-u':
  43.                 self.username = a
  44.             if o == '-p':
  45.                 self.password = a
  46.  
  47.         if(len(optargs) < 1):
  48.             raise 'Usage: ' + sys.argv[0] + ' [-u username] [-p password] server'
  49.         self.server = optargs[0]
  50.         self.args = optargs[1:]
  51.         if not isinstance(self.server, types.StringTypes):
  52.             raise 'expected server name: ', optargs
  53.  
  54.         self.__auth = {'Authorization':{'username':self.username,'password':self.password}}
  55.  
  56.         self.__xmlrpc = xmlrpclib.Server("http://" + self.server + "/RPC2")
  57.  
  58.  
Mar 11 '08 #1
Share this Question
Share on Google+
2 Replies


jlm699
100+
P: 314
Opening a file and writing to it is rather simple...

Expand|Select|Wrap|Line Numbers
  1. # Open a file for writing using a file handle
  2. fh = open('foo.bar', 'w')
  3.  
  4. # Remember to use \n for Unix and \r\n for Windows
  5. textline = 'This is a sample text line\n'
  6. details = 'Another line of text\n'
  7.  
  8. # Write the text to the file
  9. fh.write(textline)
  10. fh.write(details)
  11.  
  12. # Always remember to close your file handles!
  13. fh.close()
  14.  
  15. # Now to open file again and read
  16. fh = open('foo.bar', 'r')
  17. for line in fh:
  18.     # have to strip the whitespace to avoid double spacing output
  19.     print line.strip()
  20.  
  21. # Return 'cursor' to beginning of file
  22. fh.seek(0)
  23.  
  24. # Take in text as single block
  25. alltext = fh.read()
  26. print alltext
  27.  
  28. ''' Console output
  29. Microsoft Windows XP [Version 5.1.2600]
  30. (C) Copyright 1985-2001 Microsoft Corp.
  31.  
  32. C:\Documents and Settings\Administrator\Desktop>python fileio.py
  33. This is a sample text line
  34. Another line of text
  35. This is a sample text line
  36. Another line of text
  37. '''
  38.  
Hope that helps!
Mar 11 '08 #2

jlm699
100+
P: 314
Whenever your program receives the information, you can choose to do something like the following:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. fh = open('sys_info.txt', 'w')
  3.  
  4. sys_info = # Retrieve system information
  5. str_to_output = '%s, %s\n' % (time.strftime('%m/%d/%y, %I:%M:%S'), sys_info)
  6. fh.write(str_to_output)
  7.  
Mar 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.