Help with os.system

Grégoire Dooms dooms at info.LESS.ucl.SPAM.ac.be
Sun Jun 13 17:51:23 EDT 2004


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



More information about the Python-list mailing list