Help with os.system

Discussion in 'Python' started by Mizrandir, Jun 13, 2004.

  1. Mizrandir

    Mizrandir Guest

    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:\Documents and
    Settings\User\Escritorio\sample2e.tex" -output-directory "C:\Documents
    and Settings\User\Escritorio"')
    os.startfile('C:\Documents and Settings\User\Escritorio\sample2e.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\\miktex\\bin\\latex.exe
    (if not it does not work)? And why not in the other places?

    If I have the string "C:\Documents and
    Settings\User\Escritorio\sample2e.tex" stored in a variable, how could
    I use it?

    Thanks in advance, miz.
     
    Mizrandir, Jun 13, 2004
    #1
    1. Advertising

  2. Mizrandir wrote:
    > 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:\Documents and
    > Settings\User\Escritorio\sample2e.tex" -output-directory "C:\Documents
    > and Settings\User\Escritorio"')
    > os.startfile('C:\Documents and Settings\User\Escritorio\sample2e.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\\miktex\\bin\\latex.exe
    > (if not it does not work)? And why not in the other places?


    '\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\miktex\bin\latex.exe' will work.

    >
    > If I have the string "C:\Documents and
    > Settings\User\Escritorio\sample2e.tex" stored in a variable, how could
    > I use it?


    # let it be s:
    s = r"C:\Documents and Settings\User\Escritorio\sample2e.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\miktex\bin\latex.exe'
    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
     
    =?ISO-8859-1?Q?Gr=E9goire_Dooms?=, Jun 13, 2004
    #2
    1. Advertising

  3. Mizrandir

    Mizrandir Guest

    > 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.
     
    Mizrandir, Jun 14, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sfunds sfunds
    Replies:
    9
    Views:
    8,727
  2. Charles A. Lackman

    Streaming with System.IO and System.Net

    Charles A. Lackman, Dec 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    548
    Richard Grimes [MVP]
    Jan 15, 2005
  3. Mark Miller
    Replies:
    1
    Views:
    1,085
    Mark Miller
    Jul 18, 2003
  4. Matt
    Replies:
    2
    Views:
    838
    Matt Getting
    Jul 24, 2003
  5. keithb
    Replies:
    3
    Views:
    18,619
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 7, 2006
Loading...

Share This Page