Python batching on XP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • durumdara@mailpont.hu

    #1

    Python batching on XP

    Hi !

    I want to write a program that backup some databases in the night.

    Pseudo like this:

    try:
    if cmd('net stop dbservice'):
    s=c://backup'+str(tim e.time())+'.zip '
    if cmd('zipit c:\\database '+s):
    if cmd('map drive \\\\backupdrive \\c$ y -user BACKUP -pwd SECRET'):
    if cmd('copy '+s+' y:\\'):
    LogSucc()
    finally:
    cmd('net start dbservice')

    I'm trying with os.system() commands.
    But that is printing the result message to the screen, not to a tuple,
    like commands.|getst atusoutput().
    I need to store this message, because I want to log everything that this
    program do/print; every of the messages.

    So: how to I capture the screen, or how to I use
    |commands.|gets tatusoutput() to access the Windows batch/cmd commands,
    and get their errors/statuses ?

    Please help me !

    Thanks for it: dd
    |
  • Larry Bates

    #2
    Re: Python batching on XP

    You should take a look at the subprocess module


    -Larry Bates

    durumdara@mailp ont.hu wrote:[color=blue]
    > Hi !
    >
    > I want to write a program that backup some databases in the night.
    >
    > Pseudo like this:
    >
    > try:
    > if cmd('net stop dbservice'):
    > s=c://backup'+str(tim e.time())+'.zip '
    > if cmd('zipit c:\\database '+s):
    > if cmd('map drive \\\\backupdrive \\c$ y -user BACKUP -pwd SECRET'):
    > if cmd('copy '+s+' y:\\'):
    > LogSucc()
    > finally:
    > cmd('net start dbservice')
    >
    > I'm trying with os.system() commands.
    > But that is printing the result message to the screen, not to a tuple,
    > like commands.|getst atusoutput().
    > I need to store this message, because I want to log everything that this
    > program do/print; every of the messages.
    >
    > So: how to I capture the screen, or how to I use
    > |commands.|gets tatusoutput() to access the Windows batch/cmd commands,
    > and get their errors/statuses ?
    >
    > Please help me !
    >
    > Thanks for it: dd
    > |[/color]

    Comment

    • durumdara@mailpont.hu

      #3
      Re: Python batching on XP

      Hi !

      I have Py 2.3.5, and subprocess is placed in version 2.4.

      The os.popen is not good, because it is not get return value. I can read
      the return value, but the message is os dependent (XP, 2k, NT).
      I create this function. It is not too good, but it is working:

      def Cmd(cmd,tmpdir= "./"):
      outfn='%s/_out.msg'%tmpdi r
      errfn='%s/_err.msg'%tmpdi r
      post="1>%s 2>%s"%(outfn,er rfn)
      cmd2='%s %s'%(cmd,post)
      r=os.system(cmd 2)
      f=open(outfn,"r ")
      try:
      s1=f.read()
      finally:
      f.close()
      f=open(errfn,"r ")
      try:
      s2=f.read()
      finally:
      f.close()
      s="\n".join([s1,s2]).strip()
      return (r,s)

      Anyone have a better idea ?

      Thanks for it: dd



      Larry Bates wrote:
      [color=blue]
      >You should take a look at the subprocess module
      >http://www.python.org/dev/doc/devel/...ubprocess.html
      >
      >-Larry Bates
      >
      >durumdara@mail pont.hu wrote:
      >
      >[color=green]
      >>Hi !
      >>
      >>I want to write a program that backup some databases in the night.
      >>
      >>Pseudo like this:
      >>
      >>try:
      >>if cmd('net stop dbservice'):
      >> s=c://backup'+str(tim e.time())+'.zip '
      >> if cmd('zipit c:\\database '+s):
      >> if cmd('map drive \\\\backupdrive \\c$ y -user BACKUP -pwd SECRET'):
      >> if cmd('copy '+s+' y:\\'):
      >> LogSucc()
      >>finally:
      >> cmd('net start dbservice')
      >>
      >>I'm trying with os.system() commands.
      >>But that is printing the result message to the screen, not to a tuple,
      >>like commands.|getst atusoutput().
      >>I need to store this message, because I want to log everything that this
      >>program do/print; every of the messages.
      >>
      >>So: how to I capture the screen, or how to I use
      >>|commands.|ge tstatusoutput() to access the Windows batch/cmd commands,
      >>and get their errors/statuses ?
      >>
      >>Please help me !
      >>
      >>Thanks for it: dd
      >>|
      >>
      >>[/color][/color]

      Comment

      Working...