1

I'm using the "sh" module in python in order to call external commands on Linux. In my particular case I would like to call the "du" command because it is more efficient than doing such calculations "by hand". Unfortunately the following line does not work:

output = sh.du('-sx', '/tmp/*')

But this does work:

output = sh.du('-sx', '/tmp/')

If I pass an asterisk I get the following error message:

'ascii' codec can't encode character u'\u2018' in position 87: ordinal not in range(128)

Does anyone know how to deal with asterisks in command line arguments?


As requested, here is the stack trace:

Traceback (most recent call last):
  File "./unittest.py", line 33, in <module>
    output = sh.du('-sx', '/tmp/*')
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 1021, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 486, in __init__
    self.wait()
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 500, in wait
    self.handle_command_exit_code(exit_code)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 516, in handle_command_exit_code
    raise exc(self.ran, self.process.stdout, self.process.stderr)
sh.ErrorReturnCode_1
Regis May
  • 3,070
  • 2
  • 30
  • 51
  • 1
    Are there files that use any characters above the ASCII range in your temp folder? And the full traceback would be useful. – Niklas R Oct 03 '15 at 13:44
  • You forgot the most important part of the exception, the part telling you *No such file or directory* – Padraic Cunningham Oct 03 '15 at 14:01
  • Not in /tmp but I assume somewhere else. But this should not be a problem because I run "du" with the "-s" option. '/tmp' was just an example here. Within my output there is no character at all that will exceed the ASCII range. – Regis May Oct 03 '15 at 14:21
  • @RegisMay, it has nothing to do with char, it happens for every directory as you are passing a wildcard which is not interpreted as one, you can run it anywhere and you will get the same output. lastly unless you run the script with sudo you probably won't have permissions to read from /tmp/ so you will see an error there regardless – Padraic Cunningham Oct 03 '15 at 14:29

2 Answers2

3

The answer is:

I forgot that asterisk processing is done by bash itself, not by "du". Which means: "sh" indeed behaves correcly and exactly as one should expect. It passes "/tmp/*" directly to "du" (but bash does not). That's what Padraic Cunningham tried to tell me.

Therefor the ONLY correct way to do what I'm trying to do is the way the user rmn described:

sh.du('-sx', sh.glob('/tmp/*'))

And this is exactly what the bash shell itself does when calling "du".

Regis May
  • 3,070
  • 2
  • 30
  • 51
2

use sh.glob

sh.du('-sx', sh.glob('/tmp/*'))
r-m-n
  • 14,192
  • 4
  • 69
  • 68