OSError: [Errno 2] No such file or directory while using python subprocess in Django

前端 未结 3 1423
不知归路
不知归路 2020-11-28 20:12

I am trying to run a program to make some system calls inside Python code using subprocess.call() which throws the following error:

Traceback (m         


        
相关标签:
3条回答
  • 2020-11-28 20:30

    Use shell=True if you're passing a string to subprocess.call.

    From docs:

    If passing a single string, either shell must be True or else the string must simply name the program to be executed without specifying any arguments.

    subprocess.call(crop, shell=True)
    

    or:

    import shlex
    subprocess.call(shlex.split(crop))
    
    0 讨论(0)
  • 2020-11-28 20:40

    Can't upvote so I'll repost @jfs comment cause I think it should be more visible.

    @AnneTheAgile: shell=True is not required. Moreover you should not use it unless it is necessary (see @ valid's comment). You should pass each command-line argument as a separate list item instead e.g., use ['command', 'arg 1', 'arg 2'] instead of "command 'arg 1' 'arg 2'". – jfs Mar 3 '15 at 10:02

    0 讨论(0)
  • No such file or directory can be also raised if you are trying to put a file argument to Popen with double-quotes.

    For example:

    call_args = ['mv', '"path/to/file with spaces.txt"', 'somewhere']
    

    In this case, you need to remove double-quotes.

    call_args = ['mv', 'path/to/file with spaces.txt', 'somewhere']
    
    0 讨论(0)
提交回复
热议问题