OSError: [Errno 2] 在 Django 中使用 python 子进程时没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18962785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:25:48  来源:igfitidea点击:

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

pythondjangosubprocess

提问by Sandeep Mederametla

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

我正在尝试运行一个程序来在 Python 代码中进行一些系统调用,使用subprocess.call()它会引发以下错误:

Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/usr/lib/python2.7/subprocess.py", line 493, in call
      return Popen(*popenargs, **kwargs).wait()
      File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
      raise child_exception
      OSError: [Errno 2] No such file or directory

My actual Python code is as follows:

我的实际 Python 代码如下:

url = "/media/videos/3cf02324-43e5-4996-bbdf-6377df448ae4.mp4"
real_path = "/home/chanceapp/webapps/chanceapp/chanceapp"+url
fake_crop_path = "/home/chanceapp/webapps/chanceapp/chanceapp/fake1"+url
fake_rotate_path = "/home/chanceapp/webapps/chanceapp.chanceapp/fake2"+url
crop = "ffmpeg -i %s -vf "%(real_path)+"crop=400:400:0:0 "+ "-strict -2 %s"%(fake_crop_path)
rotate = "ffmpeg -i %s -vf "%(fake_crop_path)+"transpose=1 "+"%s"%(fake_rotate_path)
move_rotated = "mv"+" %s"%(fake_rotate_path)+" %s"%(real_path)
delete_cropped = "rm "+"%s"%(fake_crop_path)
#system calls:
subprocess.call(crop)

Can I get some relevant advice on how to solve this?

我能得到一些有关如何解决这个问题的相关建议吗?

采纳答案by Ashwini Chaudhary

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

使用shell=True,如果你传递一个字符串subprocess.call

From docs:

文档

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

如果传递单个字符串,要么shell必须是True要么字符串必须简单地命名要执行的程序而不指定任何参数。

subprocess.call(crop, shell=True)

or:

或者:

import shlex
subprocess.call(shlex.split(crop))

回答by Dusan Maksic

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

无法投票,所以我会重新发布@jfs 评论,因为我认为它应该更明显。

@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

@AnneTheAgile:shell=True 不是必需的。此外,除非有必要,否则不应使用它(请参阅@valid 的评论)。您应该将每个命令行参数作为单独的列表项传递,例如,使用 ['command', 'arg 1', 'arg 2'] 而不是“command 'arg 1' 'arg 2'”。– jfs 2015 年 3 月 3 日 10:02

回答by Daniil Mashkin

No such file or directorycan be also raised if you are trying to put a file argument to Popenwith double-quotes.

No such file or directory如果您尝试Popen使用双引号将文件参数放入,也可以引发。

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']