What do you guys do when writing scripts that use shell commands? I'm PHPer, so I want to write in php, but this time I played with Python.
It seems that Python has a library called sh, so I tried using it this time.
shtest.py
# -*- coding: utf-8 -*-
from sh import ls
from sh import sort
from sh import cat
from sh import cd
cd("./texts")
ls1 = ls.bake('-1')
# ls -1
print(ls1())
#Execution result
# mike_oldfiled.txt
# miles_davis.txt
# pink_floyd.txt
# ls -1 | sort -Run r
contents = map(lambda (i,f): "(%d)%s: %s"%(i+1,f.strip().rjust(20),cat(f.strip()).strip()),enumerate(sort(ls1(),'-r')))
for content in contents:
print content
# [Execution result]
# (1) pink_floyd.txt: Echoes(16:31)
# (2) miles_davis.txt: So What(9:08)
# (3) mike_oldfiled.txt: Amarok(1:00:03)
I like the fact that shel can be used casually. You can set options from the beginning with bake.
Basically it's easy to write, but it's hard for lamdba to go one ...
Recommended Posts