python - Python 系统调用失败

标签 python linux subprocess sys

我试图通过在配置文件中允许自定义系统调用来允许自定义监控。这是一个简单的 .ini 文件,但我对得到的结果有疑问。

def sys_command(self, command):

    command_result = subprocess.check_output(["stat -c \"%s\" ./endpoint.py"], shell=True)
    return command_result

以上工作正常。

def sys_command(self, command):

    command_result = subprocess.check_output([command], shell=True)
    return command_result

以上是解析命令时不起作用的内容:

[directory]
file_size = "stat -c \"%s\" {file}"

其他函数处理 {file} 的解析、映射和字符串替换。我检查了传入的命令,它是有效的。这是我遇到的错误。

"stat -c \"%s\" ./endpoint.py"
/bin/sh: stat -c "%s" ./endpoint.py: No such file or directory
Traceback (most recent call last):
  File "monitor.py", line 147, in <module>
    print(test.custom_sys_command(test_focus_dict, "directory"))
  File "monitor.py", line 63, in custom_sys_command
    cmd_results = self.sys_command(parsed_cmd)
  File "monitor.py", line 70, in sys_command
    command_result = subprocess.check_output([command], shell=True)
  File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['"stat -c \\"%s\\" ./endpoint.py"']' returned non-zero exit status 127

第一行是解析出来传入的命令。

subprocess.CalledProcessError: Command '['"stat -c "%s" ./endpoint.py"']' returned non-zero exit status 127

以上是我删除配置文件中的转义字符时出现的错误。

最佳答案

删除 .ini 文件中的引号解决了我的问题。

“check_output 没有添加撇号,.ini 解析器将它们视为文字字符。”

关于python - Python 系统调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49561198/

相关文章:

python - 如何在 Pandas 中获取多个值

python子进程: order of output changes when using subprocess. PIPE

linux - 权限被拒绝但在服务器上添加了公钥?

java - 在 linux 中发出 kill 命令时优雅地停止嵌入式 jetty 服务器

c++ - ld undefined reference ,尽管找到了库并导出了符号

python - FFMPEG 输入中的空格阻碍执行

python - 如何使我的 python 脚本不会因错误的 Popen 命令而崩溃?

python - 使用请求发布上传的文件

python - 尽管添加了正确的范围,但带有 Python 的 Google Drive API 仍不允许文件下载

python - Windows Python 和 AWS CLI 未知编码 idna