From: svata on
Hello,

as I'm new to python I've stumbled accros os.system and its not very
well documented usage.

I use Win XP Pro and Python 2.5.

Here is the code snippet:

--------------------------------------------------------------------------------------------------

import time
import os

dir = "C:\\Documents and Settings\\somepath\\"
fileName = time.strftime("%d%m%Y")
os.system('gvim dir+fileName+".txt"')

---------------------------------------------------------------------------------------------------

The problem is that concatenated variable dir+fileName doesn't get
expanded as expected.

Is there anything I omitted?

svata

From: Sriram on
Hello svata,
It is always better to compose your string before you send it as a
command.

try printing your command string out like this :
print 'gvim dir+fileName+".txt". You'll see what the problem is.

One possible solution is to compose your command string in the
following manner:
cmd = "gvim %s%s.txt" %(dir, fileName)
and simply call os.system with cmd.
os.system(cmd)

Here is a little more detail on string format specifiers
http://docs.python.org/lib/typesseq-strings.html

HTH
Sriram

On Feb 27, 7:24 am, "svata" <svato...(a)gmail.com> wrote:
> Hello,
>
> as I'm new to python I've stumbled accros os.system and its not very
> well documented usage.
>
> I use Win XP Pro and Python 2.5.
>
> Here is the code snippet:
>
> --------------------------------------------------------------------------------------------------
>
> import time
> import os
>
> dir = "C:\\Documents and Settings\\somepath\\"
> fileName = time.strftime("%d%m%Y")
> os.system('gvim dir+fileName+".txt"')
>
> ---------------------------------------------------------------------------------------------------
>
> The problem is that concatenated variable dir+fileName doesn't get
> expanded as expected.
>
> Is there anything I omitted?
>
> svata


From: zefciu on
svata wrote:
> Hello,
>
> as I'm new to python I've stumbled accros os.system and its not very
> well documented usage.
>
> I use Win XP Pro and Python 2.5.
>
> Here is the code snippet:
>
> --------------------------------------------------------------------------------------------------
>
> import time
> import os
>
> dir = "C:\\Documents and Settings\\somepath\\"
> fileName = time.strftime("%d%m%Y")
> os.system('gvim dir+fileName+".txt"')
>
> ---------------------------------------------------------------------------------------------------
>
> The problem is that concatenated variable dir+fileName doesn't get
> expanded as expected.
>
> Is there anything I omitted?
>
> svata
>

The way you write it, Python has no idea that dir and fileName are
variables, not literal parts of the string. You should put those names
outside the string, or better us the % format operator as Sriram showed
you, or even better use os.path module. Here is the reference:
http://docs.python.org/lib/module-os.path.html

zefciu
From: zefciu on
svata wrote:
> Hello,
>
> as I'm new to python I've stumbled accros os.system and its not very
> well documented usage.
>
> I use Win XP Pro and Python 2.5.
>
> Here is the code snippet:
>
> --------------------------------------------------------------------------------------------------
>
> import time
> import os
>
> dir = "C:\\Documents and Settings\\somepath\\"
> fileName = time.strftime("%d%m%Y")
> os.system('gvim dir+fileName+".txt"')
>
> ---------------------------------------------------------------------------------------------------
>
> The problem is that concatenated variable dir+fileName doesn't get
> expanded as expected.
>
> Is there anything I omitted?
>
> svata
>

The way you write it, Python has no idea that dir and fileName are
variables, not literal parts of the string. You should put those names
outside the quotation marks, or better us the % format operator as
Sriram showed
you, or even better use os.path module. Here is the reference:
http://docs.python.org/lib/module-os.path.html

zefciu
From: Steven D'Aprano on
On Tue, 27 Feb 2007 06:24:41 -0800, svata wrote:

> Hello,
>
> as I'm new to python I've stumbled accros os.system and its not very
> well documented usage.

Documentation seems pretty good to me.

system(...)
system(command) -> exit_status

Execute the command (a string) in a subshell.

What more did you want to see?

> I use Win XP Pro and Python 2.5.
>
> Here is the code snippet:
>
> --------------------------------------------------------------------------------------------------
>
> import time
> import os
>
> dir = "C:\\Documents and Settings\\somepath\\"

I believe that Windows will accept forward slashes as directory
separators, so you can write that as:

dir = "C:/Documents and Settings/somepath/"


> fileName = time.strftime("%d%m%Y")
> os.system('gvim dir+fileName+".txt"')

This will execute the command

gvim dir+fileName+".txt"

exactly as you typed it. I assume you don't have a file called
dir+fileName+".txt"


> The problem is that concatenated variable dir+fileName doesn't get
> expanded as expected.

Why would you expect them to be expanded? Does the documentation of
os.system say that the command string will be expanded before it is passed
to the subshell?


> Is there anything I omitted?

dir = "C:/Documents and Settings/somepath/"
fileName = time.strftime("%d%m%Y")
fileName = dir + fileName + ".txt"
os.system('gvim %s' % fileName)

That builds the command string correctly before passing it to a subshell.


--
Steven