无法通过asyncio使用bash命令 [英] Unable use bash commands through asyncio

查看:50
本文介绍了无法通过asyncio使用bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的命令,我可以不和谐地调用它,这使我可以查看目录中有多少文件,但是由于某种原因,我的代码在运行该命令后就不断遇到以下错误:

Im trying to make a simple command i can call in discord that lets me see how many files are in a directory, but for some reason my code keeps hitting the following error as soon as i run the command:

 `Traceback (most recent call last):
  File "/home/pi/MusicToaster/musicbot/bot.py", line 1995, in on_message
    response = await handler(**handler_kwargs)
  File "/home/pi/MusicToaster/musicbot/bot.py", line 1822, in cmd_audiocache
    stdout=asyncio.subprocess.PIPE)
  File "/usr/local/lib/python3.5/asyncio/subprocess.py", line 212, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "/usr/local/lib/python3.5/asyncio/base_events.py", line 970, in subprocess_exec
    bufsize, **kwargs)
  File "/usr/local/lib/python3.5/asyncio/unix_events.py", line 184, in _make_subprocess_transport
    **kwargs)
  File "/usr/local/lib/python3.5/asyncio/base_subprocess.py", line 40, in __init__
    stderr=stderr, bufsize=bufsize, **kwargs)
  File "/usr/local/lib/python3.5/asyncio/unix_events.py", line 635, in _start
    universal_newlines=False, bufsize=bufsize, **kwargs)
  File "/usr/local/lib/python3.5/subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.5/subprocess.py", line 1540, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'find /home/pi/MusicToaster/audio_cache -type f | wc -l'`

我已经检查了我正在运行的命令,并且我无法进行接缝查找它为什么会有错误的原因,当我手动键入该命令时我没有任何问题:

Ive checked the command im running and i cant seam to find a reason as to why it would have an error, when i type it in manually i have no issue:

该值已增加,因为在我截取该屏幕快照时正在写入该文件夹的机器人正在运行

The value has increased because the bot that writes to that folder was writing was running while i was taking that screenshot

这是命令的代码:

async def cmd_audiocache(self, channel):
    await self.safe_send_message(channel, "hang on ill check :thinking:")
    process = await asyncio.create_subprocess_exec(
        'find /home/pi/MusicToaster/audio_cache -type f | wc -l',
        stdout=asyncio.subprocess.PIPE)
    stdout, stderr = await process.communicate()
    file_count = stdout.decode().strip()
    file_count = str(file_count)
    file_count = file_count + " songs stored"
    await self.safe_send_message(channel, file_count)
    process = await asyncio.create_subprocess_exec(
        'du /home/pi/MusicToaster/audio_cache -h',
        stdout=asyncio.subprocess.PIPE)
    stdout, stderr = await process.communicate()
    file_size = stdout.decode().strip()
    file_size = str(file_size)
    file_size = "all songs total to" + file_size
    await self.safe_send_message(channel, file_size)

请原谅该代码的混乱之处,直到我知道它可以正常工作为止,我不会整理代码.

Please excuse the messiness of that code, i dont tidy code until i know it works.

推荐答案

请注意 create_subprocess_exec :

根据一个或多个字符串参数创建一个子进程[...],其中第一个字符串指定要执行的程序,其余字符串指定程序的参数.

Create a subprocess from one or more string arguments [...] where the first string specifies the program to execute, and the remaining strings specify the program’s arguments.

create_subprocess_shell :

使用平台的"shell"语法从cmd [...]创建一个子进程.

Create a subprocess from cmd [...] using the platform’s "shell" syntax.

示例:

# Using exec
process = await asyncio.create_subprocess_exec('ls', '-l')
# Using shell
process = await asyncio.create_subprocess_shell('ls -l') 

这篇关于无法通过asyncio使用bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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