Getting the file extension from a file path in Python

Determine the file extension os.path.splitext() is used to split the path name into a pair root and ext. e.g. C:\Users\memyselfandi\projects\file_extension_test\data.csv is split into C:\Users\memyselfandi\projects\file_extension_test\data and .csv Here is a working snippet for identifying CSV and XLS: import os if os.path.isfile(input_file): file_extension = os.path.splitext(input_file)[1].lower() if file_extension == “.csv”: logger.info(“It’s a CSV”) # do something with CSV…

New Blog Post

Pipenv Easter Egg

I’ve found this little easter egg hidden in pipenv: if not environments.PIPENV_HIDE_EMOJIS: now = time.localtime() # Halloween easter-egg. if ((now.tm_mon == 10) and (now.tm_mday == 30)) or ( (now.tm_mon == 10) and (now.tm_mday == 31) ): INSTALL_LABEL = “🎃 ” # Christmas easter-egg. elif ((now.tm_mon == 12) and (now.tm_mday == 24)) or ( (now.tm_mon ==…

2021 – Advent of code – Day 3

Part 1 You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate. Each bit in the gamma rate can be determined by finding the…

2021 – Advent of code – Day 2

Part 1 Today the puzzle got a bit trickier than Day 1. The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it’s going. For example: forward 5 down 5 forward 8 up 3 down 8 forward 2 Your horizontal position and depth both start at 0.…

What is python pbr?

In Distributing your own package on PyPi I’ve talked about setuptools and twine to upload a package to the Python Package Index. PBR is a nice little add-on to simplify the setup and the deplyoment of your packages.

Argparse

Primer: Arguments vs Parameters I’m sometimes confused. Is it an argument or a parameter? So here it goes: A parameter is a variable in a function definition. When a function is called, the arguments are the data you pass into the function’s parameters. The same goes for a program: A program has parameters, you call…

pytest Tutorial – Part 3

Having your tests in place after reading pytest Tutorial – Part 1 and pytest Tutorial – Part 2 you certainly want to know how much of your code is actually tested by your tests. This is called coverage. Installation pip install pytest-cov Checking the coverage

pytest Tutorial – Part 2

In my last article pytest Tutorial Part 1  I showed some basic concepts of testing with pytest. Here are two additional concepts Parameterize Test parameterization follows the concept of DRY – Don’t repeat yourself Instead of writing a new testcase for every different value you annotate your test function and handover the different values. The…