So, it’s been almost a year since I’ve posted something, and it’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 of my personal projects was a script that used FFMPEG to convert media files to X.265.
While I was doing this, I stumbled upon what appears to be a common…question? oddity? something anyway.
Basically when you are getting ready to do something:
Do you check if a value/key is there first?
Do you just “try” and if it fails catch an “except” and do something else?
In my case, I was attempting to write a script that could take the base directory having the files in it directly, like:
or have the file in a folder like:
And expect to have “Season” folders
As I’m lazy, I don’t want to have two scripts to do this, or to have to modify code each time.
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)
with open(found, 'rt') as shows: for row in shows: line = row.rstrip().split('/')
When the base is /Movies we only have line = filename
When the base is /TV/Constantine/ we have line = Season and line = filename
Now that I have a list, I tried to check if element  exists
if len(line) >= 1:
and if there was something there, it worked!!
if there isn’t anything there, it errors out….
Really? There is an else statement following…if it wasn’t true why didn’t it continue to the else?
after trying different things to make this work, and not being able too, I found a reference to
Once I did this:
try: if len(line) >= 1:
It actually worked!
Now, I still don’t fully understand why LBYL didn’t work, but a lot of code I found while searching for an answer seemed to follow EAFP using try/except method.