无法通过asyncio使用bash命令
问题描述:
我试图做一个简单的命令,我可以调用不一致的命令,让我看到目录中有多少个文件,但由于某种原因,我的代码一旦运行命令:无法通过asyncio使用bash命令
`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'`
伊夫检查命令进出口运行,我不能缝找个理由,为什么它会有一个错误,当我在手动键入它,我有没有问题:
价值增加了,因为博牛逼写入到该文件夹,而我是采取这一截图
这里是代码的命令写的是运行:
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之间的区别:
从一个或多个字符串参数创建一个子[...]其中第一个字符串指定要执行的程序,剩下的字符串指定程序的参数。
创建一个从CMD一个子进程[...]使用平台的“壳”的语法。
例子:
# Using exec
process = await asyncio.create_subprocess_exec('ls', '-l')
# Using shell
process = await asyncio.create_subprocess_shell('ls -l')
所以,如果我有一个完整的shell命令的长字符串,我怎么能发送到'asyncio.create_subprocess_exec()'以防止外壳注塑?我尝试过'commandString.split()',但不像'Popen','subprocess_exec'不接受一个列表... – medley56
@ medley56过去,解决方法是使用'create_subprocess_exec(* cmdList,...) – medley56