Python for bash users: 1 minute introduction

If you can do something interesting in bash, then here’s how you do it in python.

Suppose you have this interesting bash thing…

touch 'hello world'
find -xdev . -mmin -10 | grep hello

…and you want to do some crazy python thing on it. Here’s how you do it:

#! /usr/bin/python3

import os        # the operating system

cmd=''' find -xdev . -mmin -10 | grep hello '''
for line in os.popen(cmd,'r'):   # run the command, and read each line
  print(line.strip()[1:13])      # do something interesting with it

And that’s the whole story: you don’t need to learn python: just write an ever-bigger shell script inside your python wrapper.

This entry was posted in Stuff and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *