Run shell commands in python

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

Run shell commands in python
Run shell command / python in R
Run automatic jobs in python
Run Python unittests in parallel
Check for external commands in python
Run unittests in Python (for beginners)
Run a simple algorithm in Python
How to execute external shell scripts and commands in python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Create your own Linux commands in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
Run AzureKinect in Python on Christmas Eve.
DCI in Python
quicksort in python
nCr in python
Run the Python interpreter in a script
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Easily graph data in shell and Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Make cron-like jobs run regularly in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Run Python in C ++ on Visual Studio 2017
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Type Python scripts to run in QGIS Processing
Run Google Analytics API (core v3) in python
Run Python YOLOv3 in C ++ on Visual Studio 2017
Until you run the changefinder sample in python