You can also use shlex to make sure the string is split into the correct
components.
e.g.
import shlex
task="%s %s -S scraper -M -N -R %s" \
%(sys.executable,
os.path.join(os.getcwd(),"web2py.py"),
os.path.join(request.folder, "modules", "mymodule.py"))
task = shlex.split(task.replace('\\','/'))
p=subprocess.Popen(task)
Note that shlex does not recognise the windows \ so you have to replace it
with /
On Wednesday, 4 July 2012 15:05:13 UTC+1, Jonathan Lundell wrote:
>
> On Jul 3, 2012, at 10:21 PM, suraj d wrote:
>
> I am trying to run a python script from web2py's controller function using
> subprocess.call method. But the script does not execute from there, but
> when I am running the same method from inside python shell, it runs without
> any problems.
>
> The command I wrote in web2py controller's function is:
>
> subprocess.call(['python','script.py', args])
>
> where args is a string containing command line arguments I want to pass to
> the script The command I wrote in python shell is:
>
> >>>subprocess.call(['python','script.py','args'])
>
> here I am directly passing the arguments.
>
> script.py creates multiple files in directory, but it doesn't do so with
> web2py.
>
> I have used subprocess multiple times within the web2py controller
> function without any problem and I have also tried changing the permission
> of script.py to 777, but it was not of any help either.
>
> Can anyone tell me where my mistake is?
>
> By default, subprocess.call executes your command directly. In particular,
> there's no shell involved, and no splitting of an argument string into its
> individual arguments, so you have to do that yourself. So if you have more
> than one argument in args, you'll need to split them apart:
>
> subprocess.call(['python','script.py', arg1, arg2, arg3])
>
> or
>
> subprocess.call(['python','script.py', *list_of_args])
>
> or
>
> subprocess.call(['python','script.py', args], shell=True)
>
> or teach script.py to parse a single argument into pieces.
>
> If I'm guessing wrong about what you have in args, try putting some debug
> logic into script.py and report what it's actually seeing.
>