tags:

views:

289

answers:

7

Hi,

I'm writing a small method to replace some text in a file. The only argument I need is the new text, as it is always the same file and text to be replaced.

I'm having a problem using the os.system() call, when I try to use the argument of the method

If I use a string like below, everything runs ok:

stringId = "GRRRRRRRRR"
cmd="sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' path/file.old > path/file.new"
os.system(cmd)

Now, if i try to give a string as a parameter like below, the command is not executed. I do a print to see if the command is correct, and it is. I can even execute it with success if I copy / paste to my shell

import os
def updateExportConfigId(id):
stringId = "%s" % id
cmd= "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' path/file.old > path/file.new"
print "command is " + cmd
os.system(cmd)

Does anyone knows what is wrong?

Thanks

+1  A: 

To help you debug it, try adding:

print repr(cmd)

It might be that some special characters slipped into the command that normal print is hiding when you copy and paste it.

Andre Miller
Thanks for the tip. It helps a bit...
A: 

Maybe some indentation problem?

The following works correctly:

import os

def updateExportConfigId(id):
    stringId = "%s" % id
    cmd= "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' test.dat > test.new"
    print "command is " + cmd
    os.system(cmd)


updateExportConfigId("adsf")

Also do not use reserved words (id) as variables.

wr
I already had tried that and the result is the same.Using the print repr(cmd), I can see there is a difference between these 2 cases:cmd = 'mv test.dat test.' + stringIdoutput for print repr(cmd) is: u'mv test.dat test.new'where I used the "new" string as argumentBut if cmd="sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' test.dat > test.new"output for print repr(cmd) is: u"sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=adsf/g' test.dat > test.new"I tried all string combinations using " ", ' ' , %s, ''+'', " " + " ", ...
Try actually using the same example when you explain the difference between the two cases. The only difference I can see between the two cases is that you use completely different commands. That's not very helpful.
Lennart Regebro
For correctness, `id` is not a reserved word. It is a built-in function. The point of not using them still applies.
nosklo
+1  A: 

What is wrong is that there is some difference. Yeah, I know that's not helpful, but you need to figure out the difference.

Try running this:

import os
def updateExportConfigId(id):
    stringId = "%s" % id
    cmd1 = "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' path/file.old > path/file.new"
    stringId = "GRRRRRRRRR"
    cmd2 = "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' path/file.old > path/file.new"

    print "cmd1:" , cmd1
    print "cmd2:" , cmd2
    print cmd1 == cmd2

updateExportConfigId("GRRRRRRRRR")

The code should print:

sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=GRRRRRRRRR/g' path/file.old > path/file.new
sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=GRRRRRRRRR/g' path/file.old > path/file.new
True

Thereby showing that they are exactly the same. If the last line is "False" then they are not the same, and you should be able to see the difference.

Lennart Regebro
Well, I really getting out hair..I tested and the strings are the same.The difference appears only with print repr()print repr(command1): u"sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=GRRRR/g' file.old > file.new"print repr(cmd2): "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=GRRRR/g' file.old > file.new"In this case, cmd2 is using hardcoded string and I'm able to run cmd2 with os.system() call.Thanks
Aha, so the difference is that in the first case it's unicode, and the other it's a string. And for some reason calling os.system() with unicode doesn't work then.That must mean that the variable id getting passed in to the method is unicode.
Lennart Regebro
A: 

Maybe it helps to use only raw strings.

wr
+1  A: 

So from previous answers we now know that id is a Unicode string, which makes cmd1 a Unicode string, which os.system() is converting to a byte string for execution in the default encoding.

a) I suggest using subprocess rather than os.system()

b) I suggest not using the name of a built-in function as a variable (id).

c) I suggest explicitly encoding the string to a byte string before executing:

if isinstance(cmd,unicode):
    cmd = cmd.encode("UTF-8")

d) For Lennart Regebro's suggestion add:

assert type(cmd1) == type(cmd2)

after

print cmd1 == cmd2
Douglas Leeder
A: 

Finally, I found a way to run the os.system(cmd)!

Simple trick, to "clean" the cmd string:

os.system(str(cmd))

Now, I'm able to build the cmd with all arguments I need and at the end I just "clean" it with str() call before run it with os.system() call.

Thanks a lot for your answers!

swon

+3  A: 

Obligatory: don't use os.system - use the subprocess module:

import subprocess

def updateExportConfigId(m_id, source='path/file.old', 
                             destination='path/file.new'):
    if isinstance(m_id, unicode):
        m_id = m_id.encode('utf-8')
    cmd= [
          "sed",
          ",$s/MANAGER_ID=[0-9]*/MANAGER_ID=%s/g" % m_id,  
          source,
         ]
    subprocess.call(cmd, stdout=open(destination, 'w'))

with this code you can pass the manager id, it can have spaces, quote chars, etc. The file names can also be passed to the function, and can also contain spaces and some other special chars. That's because your shell is not unnecessarly invoked, so one less process is started on your OS, and you don't have to worry on escaping special shell characters.

Another option: Don't launch sed. Use python's re module.

import re
def updateExportConfigID(m_id, source, destination):
    if isinstance(m_id, unicode):
        m_id = m_id.encode('utf-8')
    for line in source:
        new_line = re.sub(r'MANAGER_ID=\d*', 
                          r'MANAGER_ID=' + re.escape(m_id), 
                          line)
        destination.write(new_line)

and call it like this:

updateExportConfigID('GRRRR', open('path/file.old'), open('path/file.new', 'w'))

No new processes needed.

nosklo