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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sensai
    New Member
    • Feb 2008
    • 1

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

    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

    Code:
     
    import xmlrpclib
    import getopt
    import types
    import sys
    import exceptions
    
    class _Method:
        def __init__(self,send,name,*args):
            self.__send = send
            self.__name = name
            self.__fun = getattr(send, name)
            self.__args = args
        def __getattr__(self, name):
            return Method(self.__send, "%s.%s" % (self.__name, name))
        def __call__(self, *args):
            ret={}
            for x in self.__args:
                for y in x:
                    ret[y]=x[y]
            for x in args:
                for y in x:
                    ret[y]=x[y]
            ack=self.__fun(ret)
            if ack['Acknowledgement']['code'] != 200:
                raise Exception('Error in ' + self.__name, ack)
            return ack
    
    
    
    class Connection:
        def __getattr__(self, name):
            return _Method(self.__xmlrpc,name,self.__auth)
    
        def __init__(self, *args):
            optlist, optargs = getopt.getopt(args, "u:p:")
    
            self.username = 'administrator'
            self.password = 'administrator'
    
            for o,a in optlist:
                if o == '-u':
                    self.username = a
                if o == '-p':
                    self.password = a
    
            if(len(optargs) < 1):
                raise 'Usage: ' + sys.argv[0] + ' [-u username] [-p password] server'
            self.server = optargs[0]
            self.args = optargs[1:]
            if not isinstance(self.server, types.StringTypes):
                raise 'expected server name: ', optargs
    
            self.__auth = {'Authorization':{'username':self.username,'password':self.password}}
    
            self.__xmlrpc = xmlrpclib.Server("http://" + self.server + "/RPC2")
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Opening a file and writing to it is rather simple...

    [CODE=python]# Open a file for writing using a file handle
    fh = open('foo.bar', 'w')

    # Remember to use \n for Unix and \r\n for Windows
    textline = 'This is a sample text line\n'
    details = 'Another line of text\n'

    # Write the text to the file
    fh.write(textli ne)
    fh.write(detail s)

    # Always remember to close your file handles!
    fh.close()

    # Now to open file again and read
    fh = open('foo.bar', 'r')
    for line in fh:
    # have to strip the whitespace to avoid double spacing output
    print line.strip()

    # Return 'cursor' to beginning of file
    fh.seek(0)

    # Take in text as single block
    alltext = fh.read()
    print alltext

    ''' Console output
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Admini strator\Desktop >python fileio.py
    This is a sample text line
    Another line of text
    This is a sample text line
    Another line of text
    '''
    [/CODE]

    Hope that helps!

    Comment

    • jlm699
      Contributor
      • Jul 2007
      • 314

      #3
      Whenever your program receives the information, you can choose to do something like the following:
      [CODE=python]
      import time
      fh = open('sys_info. txt', 'w')

      sys_info = # Retrieve system information
      str_to_output = '%s, %s\n' % (time.strftime( '%m/%d/%y, %I:%M:%S'), sys_info)
      fh.write(str_to _output)
      [/CODE]

      Comment

      Working...