Python: Check if a File or Directory Exists

There are quite a few ways to solve a problem in programming, and this holds true especially in Python. Many times you’ll find that multiple built-in or standard modules serve essentially the same purpose, but with slightly varying functionality. Checking if a file or directory exists using Python is definitely one of those cases. Here are a few ways to check for existing files/directories and their nuances. Throughout these examples we’ll assume our current working directory has these files and […]

Read more

The Python Property Decorator

It is often considered best practice to create getters and setters for a class’s public properties. Many languages allow you to implement this in different ways, either by using a function (like person.getName()), or by using a language-specific get or set construct. In Python, it is done using @property. In this article I’ll be describing they Python property decorator, which you may have seen being used with the @decorator syntax: class Person(object): def __init__(self, first_name, last_name): self.first_name = first_name self.last_name […]

Read more

How to Send Emails with Gmail using Python

There are quite a few ways to send email with Python, whether it be through a 3rd party library like with boto and SES, or through an email protocol like SMTP. While the subject of using Python to send emails may seem like it’s been done to death, there are just so many different ways to do it and so many issues that can come up. I thought it would be helpful to write up a tutorial on how to […]

Read more

Reading and Writing JSON to a File in Python

Introduction In this article, we’ll be parsing, reading and writing JSON data to a file in Python. Over the last 5-10 years, the JSON format has been one of, if not the most, popular ways to serialize data. Especially in the web development world, you’ll likely encounter JSON through one of the many REST APIs, application configuration, or even simple data storage. Given its prevalence and impact on programming, at some point in your development you’ll likely want to learn […]

Read more

Python’s @classmethod and @staticmethod Explained

Python is a unique language in that it is fairly easy to learn, given its straight-forward syntax, yet still extremely powerful. There are a lot more features under the hood than you might realize. While I could be referring to quite a few different things with this statement, in this case I’m talking about the decorators @classmethod and @staticmethod. For many of your projects, you probably didn’t need or encounter these features, but you may find that they come in […]

Read more

Differences Between .pyc, .pyd, and .pyo Python Files

In this article we go over the Python file types .pyc, .pyo and .pyd, and how they’re used to store bytecode that will be imported by other Python programs. You might have worked with .py files writing Python code, but you want to know what these other file types do and where they come into use. To understand these, we will look at how Python transforms code you write into instructions the machine can execute directly. Bytecode and the Python […]

Read more

Flask vs Django

In this article, we will take a look at two of the most popular web frameworks in Python: Django and Flask. Here, we will be covering how each of these frameworks compares when looking at their learning curves, how easy it is to get started. Next, we’ll also be looking at how these two stands against each other with concluding by when to use one of them. Getting Started One of the easiest ways to compare two frameworks is by […]

Read more

Reading and Writing CSV Files in Python

What is a CSV File? A CSV (Comma Separated Values) file is a file that uses a certain formatting for storing data. This file format organizes information, containing one record per line, with each field (column) separated by a delimiter. The delimiter most commonly used is usually a comma. This format is so common that it has actually been standardized in the RFC 4180. However, this standard isn’t always followed and there is a lack of universal standard usage. The […]

Read more

Command Line Arguments in Python

Overview With Python being such a popular programming language, as well as having support for most operating systems, it’s become widely used to create command line tools for many purposes. These tools can range from simple CLI apps to those that are more complex, like AWS’ awscli tool. Complex tools like this are typically controlled by the user via command line arguments, which allows the user to use specific commands, set options, and more. For example, these options could tell […]

Read more

Read a File Line-by-Line in Python

Introduction Over the course of my working life I have had the opportunity to use many programming concepts and technologies to do countless things. Some of these things involve relatively low-value fruits of my labor, such as automating the error prone or mundane like report generation, task automation, and general data reformatting. Others have been much more valuable, such as developing data products, web applications, and data analysis and processing pipelines. One thing that is notable about nearly all of […]

Read more
1 42 43 44 45 46 54