{"id":1527,"date":"2023-08-28T14:20:30","date_gmt":"2023-08-28T12:20:30","guid":{"rendered":"https:\/\/www.mcgill.org.za\/stuff\/?p=1527"},"modified":"2023-08-28T15:04:47","modified_gmt":"2023-08-28T13:04:47","slug":"python-for-bash-users","status":"publish","type":"post","link":"https:\/\/www.mcgill.org.za\/stuff\/archives\/1527","title":{"rendered":"Python for bash users: 1 minute introduction"},"content":{"rendered":"\n<p>If you can do something interesting in bash, then here&#8217;s how you do it in python.<\/p>\n\n\n\n<p>Suppose you have this interesting bash thing&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch 'hello world'\nfind -xdev . -mmin -10 | grep hello<\/code><\/pre>\n\n\n\n<p>&#8230;and you want to do some crazy python thing on it.  Here&#8217;s how you do it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/usr\/bin\/python3\n\nimport os        # the operating system\n\ncmd=''' <strong>find -xdev . -mmin -10 | grep hello<\/strong> '''\nfor line in os.popen(cmd,'r'):   # run the command, and read each line\n  print(line.strip()&#91;1:13])      # do something interesting with it<\/code><\/pre>\n\n\n\n<p>And that&#8217;s the whole story: you don&#8217;t need to learn python: just write an ever-bigger shell script inside your python wrapper.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.mcgill.org.za\/stuff\/wp-content\/uploads\/2023\/08\/python.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"416\" src=\"https:\/\/www.mcgill.org.za\/stuff\/wp-content\/uploads\/2023\/08\/python.jpg\" alt=\"\" class=\"wp-image-1530\" srcset=\"https:\/\/www.mcgill.org.za\/stuff\/wp-content\/uploads\/2023\/08\/python.jpg 577w, https:\/\/www.mcgill.org.za\/stuff\/wp-content\/uploads\/2023\/08\/python-300x216.jpg 300w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/a><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>If you can do something interesting in bash, then here&#8217;s how you do it in python. Suppose you have this interesting bash thing&#8230; &#8230;and you want to do some crazy python thing on it. Here&#8217;s how you do it: And &hellip; <a href=\"https:\/\/www.mcgill.org.za\/stuff\/archives\/1527\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1530,"comment_status":"open","ping_status":null,"sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[302,137,44,190],"class_list":["post-1527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stuff","tag-bash","tag-programming","tag-python","tag-stuff"],"_links":{"self":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/1527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/comments?post=1527"}],"version-history":[{"count":4,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/1527\/revisions"}],"predecessor-version":[{"id":1533,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/1527\/revisions\/1533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/media\/1530"}],"wp:attachment":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/media?parent=1527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/categories?post=1527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/tags?post=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}