os.system and quoted strings

S

svata

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
 
S

Sriram

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
 
Z

zefciu

svata said:
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
 
Z

zefciu

svata said:
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
 
S

Steven D'Aprano

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

svata

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 specifiershttp://docs.python.org/lib/typesseq-strings.html

HTH
Sriram

Thank you for prompt reply. I should be more concise with strings, I
think :)

svata
 
D

Dan Bishop

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

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

Windows system calls treat / and \ interchangeably, but the command
prompt insists on backslashes.
 
D

Dennis Lee Bieber

Windows system calls treat / and \ interchangeably, but the command
prompt insists on backslashes.

If one is really worried... use os.path.join() for all parts...

Though it looks like a minor bug for the top level?
.... "Documents and Settings",
.... "somepath")
'c:Documents and Settings\\somepath'
Hmmm, a quick test with

dir "e:userdata"

worked, so the top level \ may not be needed...

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Duncan Booth

Dan Bishop said:
Windows system calls treat / and \ interchangeably, but the command
prompt insists on backslashes.

No. Commands built-in to the command prompt and certain other programs
(mostly but not exclusively from Microsoft) insist on backslashes. Most
programs, especially those originating from the unixverse, will accept
backslashes and forward slashes interchangeably from the command prompt.
Even a lot of Microsoft's own programs are happy to accept backslashed
command line arguments.

If in doubt just call os.path.normpath() on a path string before using it.
Also it is usually worth putting command line arguments inside " marks to
avoid problems with spaces or other special characters in path names.
 
S

Sion Arrowsmith

Dennis Lee Bieber said:
... "Documents and Settings",
... "somepath")
'c:Documents and Settings\\somepath'

Hmmm, a quick test with

dir "e:userdata"

worked, so the top level \ may not be needed...

"drive:" is the 'cwd' on the drive, so "drive:directory" is relative
to that. Try "cd e:userdata" and repeat "dir e:userdata" and see what
happens. os.path.join() has to behave as above, otherwise you wouldn't
be able to use it to construct a relative path like that.

(And since I don't think anyone's mentioned it in this thread yet,
subprocess.call() instead of os.system().)
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top