Catch Exception While Using Python with Statement

Sarika Singh
Updated on 08-Jun-2025 14:31:34

167 Views

In Python, the with statement is used when working with files or network connections. It makes sure that resources (files) are opened, used, and then closed properly, even if an error occurs during the process. If you want to catch any exceptions that happen inside a with block, you can wrap it in a try-except statement. Alternatively, you can use a custom context manager that handles exceptions on its own. Catching Exceptions Inside with Block Using try-except To catch errors that occur inside a with block, we need to place it within a try-except. This helps you to handle any ... Read More

Get Maximum File Name Length Limit using Python

Niharikaa Aitam
Updated on 08-Jun-2025 12:56:40

3K+ Views

In Python, when performing file operations such as creating or renaming files, one of the limitations is the maximum length limit of a file name, and when we exceed this limit, then results in errors such as OSError: [Errno 36] File name too long. Using os.pathconf() function In Python, the os.pathconf() function is used to get system-specific configuration values related to files and directories. This function also helps in checking limits such as the maximum file name length or the maximum path length allowed by the file system. Syntax Following is the syntax of using the os.pathconf() function - os.pathconf(path, name) ... Read More

Match a Word in Python Using Regular Expression

Niharikaa Aitam
Updated on 08-Jun-2025 12:44:29

5K+ Views

In Python, Regular Expressions (RegEx) are used to match patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering, and much more based on string patterns. In this article, we will explore different ways to match a word in Python using the re module. Match a Specific Word using re.search() In Python, the re.search() method is used to search for a pattern within a given string. If the pattern is found, then it returns a match object otherwise, it returns ... Read More

Match Start and End in Python's Regex

Niharikaa Aitam
Updated on 08-Jun-2025 11:22:07

2K+ Views

In Python, the re module is used to work with regular expressions. In some cases, we need to check whether a string starts or ends with a specific pattern, and then we need to use special regex characters called anchors. Following are the anchors used in regular expressions - ^ − This anchor matches the beginning of a string. $ − This anchor matches the end of a string. Python Regex Methods The following methods from the re module are commonly used to apply start and end matching - ... Read More

Match Non-Word Characters in Python Using Regular Expression

Niharikaa Aitam
Updated on 08-Jun-2025 10:26:33

2K+ Views

A Non-word character is any type of character that is not a letter, digit, or underscore, i.e., anything other than [a-z, A-Z, 0-9_]. Examples of non-word characters are symbols, punctuation marks, spaces, tabs, and other special characters. In Python, Regular Expressions (RegEx) are used to match the patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering and much more, based on string patterns. To match a non-word character in Python, we can use the special character class \W inside ... Read More

Define a Python Dictionary Within a Dictionary

Niharikaa Aitam
Updated on 07-Jun-2025 22:46:35

277 Views

A Dictionary in Python is a mutable, unordered collection of data in a key-value format. A dictionary can also contain another dictionary as a value, and this is known as a Nested Dictionary or a dictionary within a dictionary. In this article, we are going to explore all the different ways to use a dictionary within a dictionary in Python. Syntax to define a Dictionary within another Following is the syntax of creating a Dictionary within another Dictionary in Python - outer_dict = { 'key1': { 'inner_key1': ... Read More

Recursively Iterate a Nested Python Dictionary

Niharikaa Aitam
Updated on 07-Jun-2025 22:42:30

10K+ Views

A Dictionary in Python is a mutable, unordered collection of data in a key-value format. A dictionary can also contain another dictionary as a value and this is known as a Nested Dictionary or a dictionary within a dictionary. When working with this type of data structure, it’s often important to go through each key-value pair at every level. In this article, we will explore how to recursively iterate through a nested Python dictionary using a function-based approach. Syntax to iterate through a Dictionary recursively Following is a sample syntax for recursively iterating through a nested dictionary - def recursive_iter(d): ... Read More

Simplest Way to SSH Using Python

Niharikaa Aitam
Updated on 07-Jun-2025 22:28:13

6K+ Views

SSH is referred as secure shell which is useful to remotely manage computers in a secure manner. To connect to a server, we typically use PuTTy, MobaXTerm or the command-line ssh application. Every Unix, Linux and Mac server includes SSH as standard equipment and it is usable in every data centre. SSH connections have made it feasible for secure remote access to resources, remote command execution, the transmission of software patches and updates and other administrative or management tasks. SSH is used in systems administration and file transfer software as well as to handle routers, server hardware, virtualization platforms and ... Read More

Are Python Exceptions Runtime Errors?

Sarika Singh
Updated on 07-Jun-2025 22:13:54

966 Views

Are Python Exceptions Runtime Errors? Yes, Python exceptions are considered runtime errors. Most exceptions occur during runtime.Exceptions in Python are errors that occur when there is an abnormal scenario during the execution of a program, which terminates the execution abruptly, interrupting its normal flow. Examples of exceptions are ZeroDivisionError, IndexError,  ValueError, etc..Whereas, errors in Python are detected before the execution of the program, they include syntactical errors and other violations of the program rules. Examples of errors are SyntaxError,  IndentationError,  etc.  Exceptions are Raised at Runtime Exceptions occur when the execution of the program is interrupted because of issues like logical ... Read More

Catch FloatingPointError Exception in Python

Sarika Singh
Updated on 07-Jun-2025 20:25:51

2K+ Views

Catching FloatingPointError Exception in PythonFloatingPointError in Python is an exception that occurs when there is an error in floating-point calculations. By default, Python does not raise this error for basic operations like dividing by zero; instead, it returns inf or nan. To catch this error, you need to enable it explicitly using the numpy module. In this article, you will learn how to catch a FloatingPointError by enabling it through NumPy settings and handling it using a try-except block. When Does FloatingPointError Occur? FloatingPointError can occur in cases like - Divide by zero in floating-point calculations (if enabled) ... Read More

Advertisements