OneRaynyDay
OneRaynyDay

Reputation: 3968

Python `sh` module does not recognize pattern matching

For some reason, when I run something like:

$ du -sh /some/regex.*/over?/here.txt

it gives the correct answer, correctly going through the subdirectories and returning an answer.

Strangely enough, using the sh module in python, I run the same command:

import sh
print(sh.du("-sh", "rsync_perf/d*/xa*"))

and it gives me the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 1427, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 774, in __init__
    self.wait()
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 792, in wait
    self.handle_command_exit_code(exit_code)
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 815, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_1:

  RAN: /usr/bin/du -sh rsync_perf/d*/xa*

  STDOUT:


  STDERR:
/usr/bin/du: cannot access 'rsync_perf/d*/xa*': No such file or directory

It is also not a relative vs. absolute path issue:

print(sh.du("-sh", "/home/rzhang2/secdata/analysis/rsync_perf/d*/xa*"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 1427, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 774, in __init__
    self.wait()
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 792, in wait
    self.handle_command_exit_code(exit_code)
  File "/home/rzhang2/anaconda2/lib/python2.7/site-packages/sh.py", line 815, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_1:

  RAN: /usr/bin/du -sh /home/rzhang2/secdata/analysis/rsync_perf/d*/xa*

  STDOUT:


  STDERR:
/usr/bin/du: cannot access '/home/rzhang2/secdata/analysis/rsync_perf/d*/xa*': No such file or directory

Note: Running either versions in bash in the same directory gives me the answer I wanted.

Upvotes: 0

Views: 252

Answers (1)

abarnert
abarnert

Reputation: 365953

Looking at the documentation for this module, it doesn't claim to support bash-style glob expressions the same way bash does. In fact, it doesn't support globs at all:

Glob expansion is a feature of a shell, like Bash, and is performed by the shell before passing the results to the program to be exec’d. Because sh is not a shell, but rather tool to execute programs directly, we do not handle glob expansion like a shell would.

So in order to use "*" like you would on the commandline, pass it into glob.glob() first:

import sh
import glob
sh.ls(glob.glob("*.py"))

Upvotes: 3

Related Questions