os.system vs subrocess.call

Peter Otten __peter__ at web.de
Thu Nov 28 05:48:30 EST 2019


Ulrich Goebel wrote:

> Hi,
> 
> I have to call commands from inside a python skript. These commands are
> in fact other python scripts. So I made
> 
>      os.system('\.Test.py')
> 
> That works.
> 
> Now I tried to use
> 
>      supprocess.call(['.\', 'test.py'])

Remember to use cut and paste for code and traceback.
The above triggers a syntax error because the backslash escapes the ending ' 
of the first argument. Also: is it Test.py or test.py?

> 
> That doesn't work but ends in an error:
> 
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "/usr/lib/python3.5/subprocess.py", line 557, in call
>      with Popen(*popenargs, **kwargs) as p:
>    File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
>      restore_signals, start_new_session)
>    File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
>      raise child_exception_type(errno_num, err_msg)
> PermissionError: [Errno 13] Permission denied
> 
> Using
> 
>      subprocess.call(['./', 'Test.py'], shell=True)
> 
> I get
> 
> Test.py: 1: Test.py: ./: Permission denied
> 
> Is there a simple way to use subprocess in this usecase?

You must not split the path into directory and name.
If you are on Linux or similar, your script is executable, and your file 
system is case sensitive:

$ echo -e '#!/usr/bin/python3\nprint("hello")' > test.py
$ chmod u+x test.py
$ cat test.py
#!/usr/bin/python3
print("hello")
$ python3
Python 3.4.3 (default, Nov 12 2018, 22:25:49) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["./test.py"])
hello
0




More information about the Python-list mailing list