不能管理使用Python中的子模块(没有这样的文件) [英] Can't manage to use subprocess module in Python (no such file)

查看:168
本文介绍了不能管理使用Python中的子模块(没有这样的文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的蟒蛇code启动一个bash脚本。
假设该文件containting的bash脚本被称为/home/myname/myfile.sh,我已经试过:

I'm trying to launch a bash script in my python code. Assuming the file containting the bash script is called "/home/myname/myfile.sh", I've tried :

import subprocess
subprocess.call(['/home/myname/myfile.sh'])

但我得到:

Traceback (most recent call last):
  File "run_splitseq.py", line 21, in <module>
    subprocess.call(["/home/myname/myfile.sh"])
  File "/home/myname/.pythonbrew/pythons/Python-2.6.5/lib/python2.6/subprocess.py", line 480, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/home/myname/.pythonbrew/pythons/Python-2.6.5/lib/python2.6/subprocess.py", line 633, in __init__
    errread, errwrite)
  File "/home/myname/.pythonbrew/pythons/Python-2.6.5/lib/python2.6/subprocess.py", line 1139, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

当我尝试:

subprocess.call(['/home/myname/myfile.sh'], shell=True)

我得到:

/bin/sh: 1: /home/myname/myfile.sh: not found

Whatsmore我喜欢把争论,在网上搜索,我发现这种方式:

Whatsmore I'd like to put arguments, searching on the net I've found this way :

subprocess.call(['/home/myname/myfile.sh', arg1], shell=True)

但它仍然因为我得到不工作:

But it still doesn't work since I get :

arg1: 1: arg1: /home/myname/myfile.sh: not found

谁能帮帮我吗? :)

Can anyone please help me ? :)

推荐答案

我不知道是什么原因导致你的问题。不过,我已经看到了一些东西,我很担心:

I'm not sure what causes your problem. But I've seen some things I'm concerned about:


  1. subprocess.call()你应该给任何字符串列表,第一个是可执行文件的路径,其余单身的参数;那么你应该使用壳=假(或者忽略它,因为这是默认的)。的或者的你一起壳= TRUE 通过,那么你应该通过只有一个字符串(而不是一个字符串列表!),这将是间preTED并通过外壳裂开,所以它应该包含可执行文件路径和所有参数(中引用的版本,如果需要的话)。

  1. to subprocess.call() you should give either a list of strings, the first being the path to the executable and the remaining being single arguments; then you should use shell=False (or omit it as this is the default). Or you pass along shell=True, then you should pass exactly one string (not a list of strings!) which will be interpreted and split by a shell, so it should contain the path to the executable and all arguments (in quoted versions, if necessary).

所以它要么

subprocess.call(['/路径/要/可执行文件,ARG1,ARG2'])#SHELL =假

subprocess.call('/路径/要/可执行文件ARG1ARG2',壳=真)

错误消息未找到可由于多种原因而发生。

The error message not found can occur because of several reasons.

一个。 (;没有尝试壳= TRUE 与字符串列表,例如)

a. Wrong way of specifying the path to the executable (shell=True with list of string, for instance; didn't try that).

乙。可执行文件本身不存在(但你检查,你说的)。

b. The executable itself does not exist (but you checked for that you said).

℃。可执行文件不能运行(权限位丢失)。

c. The executable isn't executable (permission bits missing).

Ð。该可执行文件在其第一行指定一个跨preTER(使用#!),这不存在(如#!/ bin / sh的或类似)。

d. The executable specifies in its first line an interpreter (using #!) which does not exist (e. g. #!/bin/sh or similar).

有没有任何这些申请?

这篇关于不能管理使用Python中的子模块(没有这样的文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆