May-30-2018, 08:43 PM
(May-30-2018, 08:11 PM)volcano63 Wrote: Command string as parameter may only be provided when it is coupled withshell=True
; with default valueFalse
list is required (as you have shown)
I try not to use the shell=True mode... some bad experiences with the shell expansions.
I have done some additional tests and you are right that when you want to pass parameters with shell=False you need to pass them in a list. Is logical as Popen will try to blindly search for a file with the full name:
>>>with subprocess.Popen(['ls'], stdout=subprocess.PIPE) as p: >>> print(''.join(b.decode() for b in p.stdout.readlines())) cpt.py subp.py >>> with subprocess.Popen('ls', stdout=subprocess.PIPE) as p: >>> print(''.join(b.decode() for b in p.stdout.readlines())) cpt.py subp.py >>> with subprocess.Popen(['ls', '-r'], stdout=subprocess.PIPE) as p: >>> print(''.join(b.decode() for b in p.stdout.readlines())) subp.py cpt.py >>> with subprocess.Popen('ls -r', stdout=subprocess.PIPE) as p: >>> print(''.join(b.decode() for b in p.stdout.readlines())) Traceback (most recent call last): File "subp.py", line 27, in <module> with subprocess.Popen('ls -r', stdout=subprocess.PIPE) as p: File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'ls -r': 'ls -r'But for a python script that is in the same folder works in both ways as long as you set it executable.
If the script is not executable it produces an error like:
Error:Traceback (most recent call last):
File "subp.py", line 7, in <module>
with subprocess.Popen(['./cpt.py'], stdout=subprocess.PIPE) as p:
File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: './cpt.py'