Not Available
Not Available

Reputation: 3355

os.execv without args argument

I want to replace the current process with a new one using os.execv, this works fine unless you don't have any arguments.

How can I call this even if I don't have an arguments to pass to the process I want to launch ?

# Works fine, unless the arguments tuple wouldn't exist or be empty
os.execv('process.exe', ('arg1', 'arg2'))

Upvotes: 9

Views: 24307

Answers (3)

phobie
phobie

Reputation: 2564

argv[0] should be a string with at least one character. These three variants can resolve the problem:

cmd = '/usr/bin/vi'
os.execv(cmd, (' ',))
os.execv(cmd, [' '])
os.execl(cmd, ' ')

Usually, the first parameter of an argument list (sys.argv) is the command which had been used to invoke the application. So it is better to use one of those:

cmd = '/usr/bin/vi'
os.execv(cmd, (cmd,))
os.execv(cmd, [cmd])
os.execl(cmd, cmd)

os.exec* documentation on python.org

Upvotes: 15

Not Available
Not Available

Reputation: 3355

Okay, after asking on IRC they pointed out why it works this way.

The first argument (arg0) is normally the filename of what you're executing (sys.argv[0] for example), so the first argument should always be the filename.

This explains why the arguments aren't optional, on IRC they said that arg0 is what the app will think its name is.

Upvotes: 7

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340311

This works for me

os.execv('process',())

are you sure your process work without arguments?

Or try execl

os.execl('process')

Upvotes: 1

Related Questions