5 ms·
> but ls, grep and cat are not among them. Really? Let me see you write this in native python with the same level of conciseness as the shell can pull off.
by ur-whale 3y ago
> but ls, grep and cat are not among them.
Really?
Let me see you write this in native python with the same level of conciseness as the shell can pull off.
- cuu508 3y agofrom pathlib import Path for path in Path().glob("*test*"): print(path.name)
- zinodaur 3y agoi think your python version of `ls` is missing a few options
- cuu508 3y agoThis is a pure-python version of the first example in shshsh's README. The example uses ls without any arguments.
- ur-whale 3y agoSo that's ls Two characters instead of 3 lines Where's grep ? And where's the pipe ?
- d0mine 3y agoglob plays grep's role here. No need for a pipe. If you want to run a shell pipeline that is more challenging to port to pure Python, then you could the shell directly: S.run("a | b", shell=True) You could emulate it in Python using the subprocess module but it would be more verbose. It depends on specific use-case whether it is worth the trouble. Calling the shell pipeline from Python gets you the best of both worlds: a shell one liner is used as a concise domain specific language to run commands , and more complex logic, proper error handling, etc is left to Python.