Skip to content Skip to sidebar Skip to footer

Using Os.system In Python To Run Program With Parameters

How do I get python to run sudo openvpn --cd /etc/openvpn --config client.ovpn I'm trying the following at the minute without success vpnfile2 = '/etc/init.d/openvpn' cfgFile = 'cl

Solution 1:

use the subprocess module

import subprocess
subprocess.call(['sudo', vpnFile2, '--cd', vpnpath, '--config', cfgFile])

Solution 2:

This question has been posted awhile ago, but if someone comes across this (like me), there is another way to get privileges using the os.system method ..

It will only work if you are running this in a GUI environment. You can simply try to call it with 'gksu' or ('kdesu' or 'kdesudo'), it would look like this in a gnome session:

import os
vpnfile2 = '/etc/init.d/openvpn'
cfgFile = 'client.ovpn'

os.system('gksu \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"')

The prompt works, but I have not tested it to work with your code.


Solution 3:

If there is some reason you want to use os.system, instead of subprocess, I usually wash it through bash, so os.system('''sudo bash -c "command to run"''') (Or sh or whatever shell you have). It processes the arguments better in many cases.


Post a Comment for "Using Os.system In Python To Run Program With Parameters"