Help with os.system

M

Mizrandir

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.
 
?

=?ISO-8859-1?Q?Gr=E9goire_Dooms?=

Mizrandir said:
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
 
M

Mizrandir

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top