3 ms·
> Please show me how to do `ls | grep -i old` in Python? for filename in os.listdir(os.curdir): if 'old' in filename.lower(): print(filename) One
by jcranmer 5y ago
> Please show me how to do `ls | grep -i old` in Python?
for filename in os.listdir(os.curdir):
if 'old' in filename.lower():
print(filename)
One of the advantages of using a language like Python is you don't need to set up a lengthy pipeline of program executions just to reformat program output. In my personal experience using Python for basic automation scripting, I just haven't really come across any need for me to pipe output from one command into another.
The problem I have with a lot of shells is that their variable syntax is almost completely broken if you want to do anything slightly harder than trivial. A recent example I had was trying to write a script that reads a shell and executes the last line with some extra arguments. So that should be `PROG=$(tail -n1 commands.sh); creduce-script "$PROG"`, nothing hard about that... except the last line was `clang "-DFOO= " bar.c`.
- nixpulvis 5y agoI'm not arguing that something like Python is nice for when I'm writing scripts out and saving/committing them. A shell is a REPL designed to use quickly and easily for one-off jobs. Shell scripts are also sometimes just simpler to string together than referencing a whole new language.