How to use Python shutil

Sometimes you want to delete a folder in Python. You know that os.rmdir() can do that for you. But then you realize it can only remove empty folders. If you want to remove the content before deleting the folder you come up with: def remove_non_empty_dir(path_to_dir): if os.path.exists(path_to_dir): files_in_dir = os.listdir(path_to_dir) for file in files_in_dir: os.remove(os.path.join(path_to_dir,…

Logging in Python – Cheat Sheet

I’ve written a piece about logging Log4j2 for Kotlin and the motivation to use logging in Python is the same: Logging is a common good practice in software engineering. It enables you to monitor applications in production to gather information about crashes and other malfunctions for further analysis. It is the “little brother” of debugging…

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 ==…

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.