{"id":323,"date":"2018-02-15T11:10:19","date_gmt":"2018-02-15T17:10:19","guid":{"rendered":"http:\/\/54.186.50.233\/?p=323"},"modified":"2018-02-15T11:10:19","modified_gmt":"2018-02-15T17:10:19","slug":"lbyl-vs-eafp","status":"publish","type":"post","link":"http:\/\/www.wifijanitor.com\/?p=323","title":{"rendered":"LBYL vs EAFP"},"content":{"rendered":"<p>So, it&#8217;s been almost a year since I&#8217;ve posted something, and it&#8217;s been a busy year.<\/p>\n<p>ERATE twice, pushing my CCIE recert to the edge of losing it (23 days left in suspension when I passed), getting married to my best friend (Halloween if anyone is wondering), and yes still working to learn python.<\/p>\n<p>One of my personal projects was a script that used FFMPEG to convert media files to X.265.<br \/>\nWhile I was doing this, I stumbled upon what appears to be a common&#8230;question? oddity? something anyway.<\/p>\n<p>Look Before You Leap <a href=\"https:\/\/docs.python.org\/3.5\/glossary.html#term-lbyl\">LBYL<\/a> or Easier to Ask For Permission <a href=\"https:\/\/docs.python.org\/3.5\/glossary.html#term-eafp\">EAFP<\/a><\/p>\n<p>Basically when you are getting ready to do something:<br \/>\nDo you check if a value\/key is there first?<br \/>\nor<br \/>\nDo you just &#8220;try&#8221; and if it fails catch an &#8220;except&#8221; and do something else?<\/p>\n<p>In my case, I was attempting to write a script that could take the base directory having the files in it directly, like:<br \/>\n\/Movies\/<br \/>\nor have the file in a folder like:<br \/>\n\/TV\/Constantine\/<br \/>\nAnd expect to have &#8220;Season&#8221; folders<\/p>\n<p>As I&#8217;m lazy, I don&#8217;t want to have two scripts to do this, or to have to modify code each time.<\/p>\n<p>So the first thing I did was use split to create a list.(ok technically the second thing I did, as the first thing was to create a list of files that matched my criteria)<\/p>\n<pre><code>with open(found, 'rt') as shows:\nfor row in shows:\nline = row.rstrip().split('\/')\n<\/code><\/pre>\n<p>When the base is \/Movies we only have line[0] = filename<br \/>\nWhen the base is \/TV\/Constantine\/ we have line[0] = Season and line[1] = filename<\/p>\n<p>Now that I have a list, I tried to check if element [1] exists<\/p>\n<pre><code>if len(line[1]) &gt;= 1:\n<\/code><\/pre>\n<p>and if there was something there, it worked!!<br \/>\nif there isn&#8217;t anything there, it errors out&#8230;.<\/p>\n<p>Really? There is an else statement following&#8230;if it wasn&#8217;t true why didn&#8217;t it continue to the else?<br \/>\nafter trying different things to make this work, and not being able too, I found a reference to<\/p>\n<p>Once I did this:<\/p>\n<pre><code>try:\nif len(line[1]) &gt;= 1:\n<\/code><\/pre>\n<p>It actually worked!<\/p>\n<p>Now, I still don&#8217;t fully understand why LBYL didn&#8217;t work, but a lot of code I found while searching for an answer seemed to follow EAFP using try\/except method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, it&#8217;s been almost a year since I&#8217;ve posted something, and it&#8217;s been a busy year. ERATE twice, pushing my CCIE recert to the edge of losing it (23 days left in suspension when I passed), getting married to my best friend (Halloween if anyone is wondering), and yes still working to learn python. One &hellip; <a href=\"http:\/\/www.wifijanitor.com\/?p=323\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;LBYL vs EAFP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[21,29,34,38],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-python","tag-eafp","tag-lbyl","tag-python","tag-scripting"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=323"}],"version-history":[{"count":0,"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wifijanitor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}