Bash in Python made easy

When I need to run bash commands or isis3 programs within a python script I turn to a neat library called sh to make my life easier and to avoid directly using the subprocess module myself. Go check it out, I also found a few other similar projects that exist and will list them below with sh.

https://plumbum.readthedocs.io/en/latest/index.html


https://amoffat.github.io/sh/

3 Likes

I find also sh incredibly useful. The standard option subprocess isn’t bad, but need a lot more boilerplate to work with and less intuitive.
Should we add some basic example comparison between the proposed solution?
Something on the line “how can I list all the file in current directory with ls -l” ?

1 Like

Yeah we could, I provided a link to the documentation that has simple examples like that but it depends on the format intended for the forum, alternatively a medium post could be made

I thought something a lá stackoverflow, with only a minimal working example, could be helpful for the reader looking for a taste of your suggestion. More that than would be hard to maintain.

Is there maybe a better example? Because ls -l really should be done with pathlib.Path('.').glob('*'); I know, it looks more work, but is actually safer for directories with several thousand entries, where ls will fail, but pathlib (based on os) doesn’t.

Thousand times YES for pathlib, I love it!! Mainly because I was using sys+os before :face_vomiting:

The point is to show a command simple enough to not get new user lost in understanding what the command should do in the first place and focus on the python+shell.
ls is well known and has a rich set of subcommand.

Do you have any other suggestion ? Maybe something that accept variable and not only options would be nice.

My idea is to have one command and list how you can use it in python with different solution, than we can always point the user to that page. Maybe a post here / wiki in github would / post on medium (that I hate more and more) would be useful.

How about “grep -l string” to get the list of files that contain “string”?

Nice one, grep is good , something like grep -f FILE -A 2 -B 2 -v -l string

This tool is super nice to explain shell commands explainshell.com - grep -f FILE -A 2 -B 2 -v -l string

Woah, how cool is that! Nice find!

Yes, this is also super nice to explain to people your command in presentation for example.