Help with os.system

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

    Help with os.system

    Hello, I'm a newbie with Python and there are some things I don't
    understand of os.system.

    I've managed to write a script that works in order to test some
    things:

    import os
    os.system('C:\\ texmf\\miktex\\ bin\\latex.exe "C:\Documen ts and
    Settings\User\E scritorio\sampl e2e.tex" -output-directory "C:\Documen ts
    and Settings\User\E scritorio"')
    os.startfile('C :\Documents and Settings\User\E scritorio\sampl e2e.dvi')

    This script launches the program "latex" and passes the input and
    output file names and directories. Afterwards it launches a dvi viewer
    to view the output.

    I have 2 questions:

    Why do I have to use double \\ in C:\\texmf\\mikt ex\\bin\\latex. exe
    (if not it does not work)? And why not in the other places?

    If I have the string "C:\Documen ts and
    Settings\User\E scritorio\sampl e2e.tex" stored in a variable, how could
    I use it?

    Thanks in advance, miz.
  • Grégoire Dooms

    #2
    Re: Help with os.system

    Mizrandir wrote:[color=blue]
    > Hello, I'm a newbie with Python and there are some things I don't
    > understand of os.system.
    >
    > I've managed to write a script that works in order to test some
    > things:
    >
    > import os
    > os.system('C:\\ texmf\\miktex\\ bin\\latex.exe "C:\Documen ts and
    > Settings\User\E scritorio\sampl e2e.tex" -output-directory "C:\Documen ts
    > and Settings\User\E scritorio"')
    > os.startfile('C :\Documents and Settings\User\E scritorio\sampl e2e.dvi')
    >
    > This script launches the program "latex" and passes the input and
    > output file names and directories. Afterwards it launches a dvi viewer
    > to view the output.
    >
    > I have 2 questions:
    >
    > Why do I have to use double \\ in C:\\texmf\\mikt ex\\bin\\latex. exe
    > (if not it does not work)? And why not in the other places?[/color]

    '\0', '\a', '\b', '\t', '\n', '\v', '\f' and '\r' are special characters.
    Generally '\*' where * is a character is called "escaped character *",
    it's a way to write/print a non-printable character.
    E.g. '\t' is the Tab character. '\\' means the backslash character.

    If you dont want the \ to be interpreted, use raw strings by prepending
    a r before your string literal: r'C:\texmf\mikt ex\bin\latex.ex e' will work.
    [color=blue]
    >
    > If I have the string "C:\Documen ts and
    > Settings\User\E scritorio\sampl e2e.tex" stored in a variable, how could
    > I use it?[/color]

    # let it be s:
    s = r"C:\Documen ts and Settings\User\E scritorio\sampl e2e.tex"
    # you can make your dir pathname by removing the last 12 chars:
    d = s[:-12]
    # better do it with os.path :
    import os.path
    d = os.path.dirname (s)
    # then the whole command with
    c = r'C:\texmf\mikt ex\bin\latex.ex e'
    comm = '%s "%s" -output-dir "%s"' % (c,s,d)
    os.system(comm)

    Read the tutorial at http://www.python.org/doc/current/tut for more ideas.

    Hope this helps
    --
    Grégoire Dooms

    Comment

    • Mizrandir

      #3
      Re: Help with os.system

      > Hope this helps

      Yes, thanks.

      The second part (storing things in vars) I had already figured out
      myself (but after posting).

      The first (about \\) I imagined it was something like that but I
      didn't know why in some places yes and not in others. But it's clear
      now.

      miz.

      Comment

      Working...