confusing behaviour of os.system

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

    #1

    confusing behaviour of os.system

    I'm trying to run the following in python.

    os.system('/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file
    \"test.c\")" ')

    This connects to xemacs with gnuclient and runs htmlize. If I run it
    from a shell, xemacs gives me an error, but it generates the html file
    just fine. If I run the same statement in python, I get the exact same
    behaviour, but there's no file.

    I'm not sure what the issue is. I thought maybe it's putting it in
    another directory or there might be some permissions problem. Any
    ideas? If you want to try it, just make sure to run "M-x
    gnuserve-start" first.

  • Ben Cartwright

    #2
    Re: confusing behaviour of os.system

    Todd wrote:[color=blue]
    > I'm trying to run the following in python.
    >
    > os.system('/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file
    > \"test.c\")" ')[/color]

    Python is interpreting the \"s as "s before it's being passed to
    os.system. Try doubling the backslashes.
    [color=blue][color=green][color=darkred]
    >>> print '/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")" '[/color][/color][/color]
    /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> "test.c")"[color=blue][color=green][color=darkred]
    >>> print '/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \\"test.c\\") "'[/color][/color][/color]
    /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")"

    --Ben

    Comment

    • Todd

      #3
      Re: confusing behaviour of os.system


      Ben Cartwright wrote:[color=blue]
      > Todd wrote:[color=green]
      > > I'm trying to run the following in python.
      > >
      > > os.system('/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file
      > > \"test.c\")" ')[/color]
      >
      > Python is interpreting the \"s as "s before it's being passed to
      > os.system. Try doubling the backslashes.
      >[color=green][color=darkred]
      > >>> print '/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")" '[/color][/color]
      > /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> "test.c")"[color=green][color=darkred]
      > >>> print '/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \\"test.c\\") "'[/color][/color]
      > /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")"
      >
      > --Ben[/color]

      Thanks! Yay multiple layers of string interpretation.

      Comment

      • Ben Finney

        #4
        Re: confusing behaviour of os.system

        "Todd" <goldfita@signa lsguru.net> writes:
        [color=blue]
        > Ben Cartwright wrote:[color=green][color=darkred]
        > > >>> print '/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")" '[/color]
        > > /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> "test.c")"[color=darkred]
        > > >>> print '/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \\"test.c\\") "'[/color]
        > > /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")"
        > >
        > > --Ben[/color]
        >
        > Thanks! Yay multiple layers of string interpretation.[/color]

        If it helps, Python has "raw string" syntax that can help alleviate this.
        [color=blue][color=green][color=darkred]
        >>> print r'/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")" '[/color][/color][/color]
        /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")"
        [color=blue][color=green][color=darkred]
        >>> filename = "test.c"
        >>> cmd_template = r'/usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"%s\")"'
        >>> command = cmd_template % filename
        >>> print command[/color][/color][/color]
        /usr/bin/gnuclient -batch -l htmlize -eval "(htmlize-file> \"test.c\")"


        --
        \ "Everything is futile." -- Marvin of Borg |
        `\ |
        _o__) |
        Ben Finney

        Comment

        Working...