0
import subprocess
subprocess.call(" python script2.py 1", shell=True)

This code does not work. It tells me permission is denied on python. Any fixes?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Firepatch
  • 3
  • 1
  • 1
    check the permissions for `script2.py` file – yash Oct 25 '17 at 03:03
  • Can you provide the full error? In case there is some other information there that you have not included so far. Also state which os you are running this on and the file permissions for `script2.py`. On linux you'd use `ls -l script2.py`. Another thing, what about removing the space at the start? – Paul Rooney Oct 25 '17 at 05:06

2 Answers2

0

I don't know if you are doing this in bash terminal but if you are you must give yourself rights to the .py file. So type this

chmod +x script2.py

Then the terminal will give you permission to it

  • I think that you only need execute permissions if you plant to execute the python file directly (i.e. it finds the python binary via the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix))). If you are executing it by invoking the interpreter yourself you don't need execute permissions. The issue is more likely something the OP has not told us about yet like the directory the script is being executed from or read permissions. – Paul Rooney Oct 25 '17 at 05:14
0

Suggestions For Using Subprocess.call()

When shell=True is dangerous?

If we execute shell commands that might include unsanitized input from an untrusted source, it will make a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of shell=True is strongly discouraged in cases where the command string is constructed from external input

Reference : When to use Shell=True for Python subprocess module

import subprocess
subprocess.call(" python script2.py 1", shell=True)

Try avoid shell=True in subprocess. Instead use

import subprocess
subprocess.call(['python','script2.py','1'])

Pass every thing as a list , avoid shell=True

import subprocess
subprocess.call('python script2.py 1'.split())

'python script2.py 1'.split() will create a list like ['python','script2.py', '1']

Addressing Permission Issue

Add execution permission to your script.

chmod +x script2.py

Fuji Komalan
  • 1,979
  • 16
  • 25