How do I execute a program from Python? os.system fails due to spaces in path

后端 未结 10 878
一生所求
一生所求 2020-11-22 08:19

I have a Python script that needs to execute an external program, but for some reason fails.

If I have the following script:

import os;
os.system(\"C         


        
10条回答
  •  孤街浪徒
    2020-11-22 08:50

    Suppose we want to run your Django web server (in Linux) that there is space between your path (path='/home// '), so do the following:

    import subprocess
    
    args = ['{}/manage.py'.format('/home// '), 'runserver']
    res = subprocess.Popen(args, stdout=subprocess.PIPE)
    output, error_ = res.communicate()
    
    if not error_:
        print(output)
    else:
        print(error_)
    

    [Note]:

    • Do not forget accessing permission: chmod 755 -R <'yor path'>
    • manage.py is exceutable: chmod +x manage.py

提交回复
热议问题