2

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:

Me running the code in from different locations

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

Here is the code for the command:

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.

1 Answer 1

12

Note the difference between 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.

and create_subprocess_shell:

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

Example:

# Using exec
process = await asyncio.create_subprocess_exec('ls', '-l')
# Using shell
process = await asyncio.create_subprocess_shell('ls -l') 
Sign up to request clarification or add additional context in comments.

3 Comments

So if I have a complete shell command as a long string, how can I send that to asyncio.create_subprocess_exec() to prevent shell injection? I tried commandString.split() but unlike Popen, subprocess_exec doesn't accept a list...
@medley56 of the past, the solution is to use create_subprocess_exec(*cmdList, ...)
@medley56 of the past, the solution is to use shlex.quote() method. If you need to call create_subprocess_exec(), then call split() function on a string. Read more here: docs.python.org/3.6/library/shlex.html#shlex.quote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.