无法通过 asyncio 使用 bash 命令

Unable use bash commands through asyncio

我想做一个简单的命令,我可以在 discord 中调用它,让我看到一个目录中有多少文件,但由于某种原因,我的代码在 运行命令:

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

我已经检查了命令 im 运行ning 并且我无法找到它为什么会出错的原因,当我手动输入它时我没有问题:

值增加了,因为写入该文件夹的机器人正在 运行ning,而我正在拍摄该屏幕截图

命令代码如下:

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)

请原谅该代码的混乱,在我知道它有效之前我不会整理代码。

注意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:

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