os.popen() not executing command on windows xp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nic

    #1

    os.popen() not executing command on windows xp

    On my system (WinXP) typing the following line into command
    prompt(cmd.exe) successfully scans the file test1.txt:
    "c:\Program Files\Grisoft\A VG Free\avgscan.ex e" "c:\program
    files\temp1\tes t1.txt"

    Yet the python script:
    import os
    a = os.popen('"c:\P rogram Files\Grisoft\A VG Free\avgscan.ex e"
    "c:\program files\temp1\tes t1.txt"')
    print a.read()

    Returns a blank line, and I doesn't scan the file. (Note I've used
    os.popen() successfully on my system in other situations like:
    os.popen('taskk ill /F /IM taskname.exe')) .

    I have a feeling my avgscan example shown above not working has
    something to do with calling a 3rd party software program (avgscan.exe)
    through popen, but I don't know why this won't work, when manually
    typing it in the command line does?

  • Gabriel Genellina

    #2
    Re: os.popen() not executing command on windows xp

    At Thursday 11/1/2007 06:42, nic wrote:
    >a = os.popen('"c:\P rogram Files\Grisoft\A VG Free\avgscan.ex e"
    >"c:\program files\temp1\tes t1.txt"')
    Your string contains backquotes, and they have to be escaped.
    Either use a raw string: os.popen(r'"c:\ Program...) or double all
    backquotes: os.popen('"c:\\ Program...)


    --
    Gabriel Genellina
    Softlab SRL






    _______________ _______________ _______________ _____
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!


    Comment

    • Justin Ezequiel

      #3
      Re: os.popen() not executing command on windows xp

      import os
      a = os.popen('"c:\P rogram Files\Grisoft\A VG Free\avgscan.ex e"
      "c:\program files\temp1\tes t1.txt"')
      print a.read()
      >
      use raw strings

      e.g., instead of '"c:...\avgscan ...'
      use r'"c:...\avgsca n...'



      Comment

      • nic

        #4
        Re: os.popen() not executing command on windows xp

        Justin Ezequiel wrote:
        import os
        a = os.popen('"c:\P rogram Files\Grisoft\A VG Free\avgscan.ex e"
        "c:\program files\temp1\tes t1.txt"')
        print a.read()
        >
        use raw strings
        >
        e.g., instead of '"c:...\avgscan ...'
        use r'"c:...\avgsca n...'
        >
        http://www.ferg.org/projects/python_...ontents_item_2
        Sorry I initally had retyped the code to replace some variables to make
        it more clear, I already had them as raw strings:

        import os
        pstr = r'"c:\Program Files\Grisoft\A VG Free\avgscan.ex e" "c:\program
        files\temp1\tes t1.txt"'
        a = os.popen(pstr)
        print a.read()

        I've confirmed the string I'm inputting to os.popen is EXACTLY the same
        as the one I can successfully execute manually in command prompt, so
        when I go:
        print pstr, it yields:
        "c:\Program Files\Grisoft\A VG Free\avgscan.ex e" "c:\program
        files\temp1\tes t1.txt"

        The problem remains popen won't execute this line as it does when
        inputted manually to command prompt.

        Comment

        • nic

          #5
          Re: os.popen() not executing command on windows xp

          It appears os.popen() doesn't like full windows paths with spaces (e.g.
          "c:/program files") so I'm going to use the subprocess module.

          Comment

          • Gabriel Genellina

            #6
            Re: os.popen() not executing command on windows xp

            At Thursday 11/1/2007 14:09, nic wrote:
            >import os
            >pstr = r'"c:\Program Files\Grisoft\A VG Free\avgscan.ex e" "c:\program
            >files\temp1\te st1.txt"'
            >a = os.popen(pstr)
            >print a.read()
            >
            >I've confirmed the string I'm inputting to os.popen is EXACTLY the same
            >as the one I can successfully execute manually in command prompt, so
            >when I go:
            >print pstr, it yields:
            >"c:\Program Files\Grisoft\A VG Free\avgscan.ex e" "c:\program
            >files\temp1\te st1.txt"
            >
            >The problem remains popen won't execute this line as it does when
            >inputted manually to command prompt.
            I have AVG so I tried this example. That's not the problem. popen
            works ok, avgscan is executed. But the avgscan program does *not* use
            stdout nor stderr to write to the console, so popen can't capture its output.
            You can confirm this at the command prompt:

            "c:\Program Files\Grisoft\A VG Free\avgscan.ex e" "c:\program
            files\temp1\tes t1.txt" >output.txt 2>&1

            You will see the usual progress messages from avgscan *on screen*,
            and when you type output.txt, it's empty. avgscan appears to write
            directly to the console.
            You may use the /REPORT option; avgscan /? for details.


            --
            Gabriel Genellina
            Softlab SRL






            _______________ _______________ _______________ _____
            Preguntá. Respondé. Descubrí.
            Todo lo que querías saber, y lo que ni imaginabas,
            está en Yahoo! Respuestas (Beta).
            ¡Probalo ya!


            Comment

            Working...