Listing files and directories using Python is a common task in many applications, from file management tools to automated scripts. However, when we are working on the Windows Operating System, the hidden files and folders are marked using specific file attributes, which can clutter the output if not handled properly. Unlike Unix-based systems, where hidden files typically start with a dot such as .hidden) whereas Windows uses metadata to mark files as hidden. In this article, we'll explore several methods to list only non-hidden files and directories. Basic Filtering by Name Prefix This is the basic Filtering method, which checks ... Read More
Python dictionaries are very difficult to handle data. They use a special system called hashing, which allows quick access to information. This specifies the cost of different operations: Time Complexities of Dictionary Operations Python dictionaries are usually fast because they use hashing to find and store data. The time complexity of dictionary operations in Python depends on the size of the dictionary and the operations performed. Here are some of the common dictionary operations - ... Read More
In Python, function names follow specific rules. A valid function name can only contain certain characters, and it must follow the naming conventions defined in the Python language syntax. Using the correct characters ensures that your code runs without syntax errors and stays readable. Allowed Characters in Function Names Python function names can consist of the following characters - Letters (A–Z, a–z) Digits (0–9) — but not at the beginning Underscores (_) — often used to separate words Function names must follow these rules - Must start with a letter or underscore Cannot start with a digit ... Read More
You can handle errors in Python using the try and except blocks. Sometimes, we also want to store the actual exception in a variable to print it or inspect it. This is done using the as keyword in modern Python. But if you have seen older code, you might find except Exception e or even except Exception, e being used. In this article, we will explore the difference between these syntaxes, understand why older ones no longer work in Python 3, and learn the best practices for clean exception handling. Understanding Basic Exception Handling Python provides try-except blocks to catch ... Read More
When writing a C extension for Python, you might need to raise an exception if something goes wrong in your C code. Python provides a special C API that helps you to raise errors in a way that works just like regular Python exceptions.This is helpful because even if your code is written in C for better performance, users can still handle errors using Python's normal try...except blocks. It makes your extension feel just like any other Python module.Using Python C API to Raise ExceptionsIn C extensions for Python, you can raise exceptions using the Python C API functions like ... Read More
In Python, the assert statement is used for debugging purposes. It tests whether a condition in your program returns True, and if not, it raises an AssertionError. This helps you catch bugs early by verifying that specific conditions are met while the code is executing. Understanding the Assert Statement The assert statement is used to verify that a given condition is true during execution. If the condition evaluates to False, the program stops and throws an AssertionError, optionally displaying a message. Example: Assertion without a message In this example, we are asserting that 5 is greater than 3, which is ... Read More
In Python, a KeyError exception occurs when a dictionary key that does not exist is accessed. This can be avoided by using proper error handling with the try and except blocks, which can catch the error and allow the program to continue running. Understanding KeyError in Python A KeyError is raised when you try to access a key that doesn't exist in a dictionary. It is one of the built-in exceptions that can be handled by Python's try and except blocks. Example: Accessing a non-existent key In this example, we are attempting to access a key that does not exist ... Read More
In Python, the try, except, and finally blocks are used to handle exceptions. These blocks allow you to catch errors that occur during the execution of a program and respond accordingly, which helps to prevent your program from crashing. Try and Except Blocks The try block contains code that might raise an exception. If an exception occurs, the except block handles it, preventing the program from crashing. Example: Handling ZeroDivisionError In this example, we are dividing a number by zero, which raises an exception and is handled by the except block - try: result = ... Read More
In Python, you need to use the built-in json module to work with JSON data. When you want to return data from a function in JSON format, you can use the json.dumps() function to convert a Python dictionary (or similar object) into a JSON string. This is helpful when making APIs, sending data in web responses, or saving organized data in files. Using json.dumps() Function The json.dumps function is used to convert a Python object (like a dictionary or list) into a JSON-formatted string. Syntax Following is its basic syntax - json.dumps(python_object) Where python_object is usually a dictionary or list ... Read More
When you write tests in Python, it is important to make sure that your function raises the correct exception for invalid input or unexpected conditions. This helps to confirm that your code handles errors properly. You can test exceptions using the following ways in python − Using the try-except blocks manually Using the unittest module Using the pytest module Using try-except Block One of the basic ways to test for exceptions is by manually using a try-except block. This allows you to execute code and catch any exceptions that occur. If the function doesn't raise the ... Read More