Sarika Singh has Published 189 Articles

Explain Try, Except and Else statement in Python.

Sarika Singh

Sarika Singh

Updated on 16-May-2025 19:45:28

1K+ Views

In Python, the try, except, and else statements are used to handle exceptions and define specific blocks of code that should execute based on whether an error occurs or not. This helps you manage errors and separate successful code from error-handling logic. Using "try" and "except" The try block contains ... Read More

How to use the ‘except’ clause with multiple exceptions in Python?

Sarika Singh

Sarika Singh

Updated on 16-May-2025 14:47:49

3K+ Views

Using "except" Clause with Multiple Exceptions It is possible to define multiple exceptions with the same except clause. It means that if the Python interpreter finds a matching exception, then it will execute the code written under the except clause. Syntax In general, the syntax for multiple exceptions is as follows - ... Read More

How to catch multiple exceptions in one line (except block) in Python?

Sarika Singh

Sarika Singh

Updated on 16-May-2025 14:39:26

446 Views

In Python, instead of writing separate except blocks for each exception, you can handle multiple exceptions together in a single except block by specifying them as a tuple. In this example, we are catching both ValueError and TypeError using a single except block - try: x = ... Read More

What are RuntimeErrors in Python?

Sarika Singh

Sarika Singh

Updated on 16-May-2025 14:29:26

843 Views

RuntimeErrors in Python are a type of built-in exception that occurs during the execution of a program. They usually indicate a problem that arises during runtime and is not necessarily syntax-related or caused by external factors.When an error is detected, and that error doesn't fall into any other specific category ... Read More

How to print all the keys of a dictionary in Python

Sarika Singh

Sarika Singh

Updated on 16-May-2025 10:46:37

107K+ Views

A Python dictionary is an unordered collection of data values. It contains a key-value pair, in contrast to other data structures that only include one value per entry. In this article, we are going to see the various ways to print all the keys of a dictionary in Python. Using dict.keys() ... Read More

What does the 'b' modifier do when a file is opened using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 18:23:46

13K+ Views

When we use the b modifier while opening a file, then the file is opened in binary mode. Any file whose format doesn't consist of readable characters is referred to as a "binary" file. Binary files include audio files like MP3s, text formats such as Word or PDF, and image files ... Read More

How to check file last access time using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 18:23:11

3K+ Views

Monitoring file access times is a common requirement for auditing, data management and cleanup of the scripts. Python provides multiple ways to retrieve the last access time of a file using the os and pathlib modules. Using os.path.getatime() Method In Python, we can use the os.path.getatime() method to retrieve a ... Read More

How to find the real user home directory using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 18:21:27

17K+ Views

When working with the Python scripts, it is required to interact with user-specific files or directories and also it's essential to accurately determine the actual user's home directory. This is especially important if the script might be executed with elevated privileges like using sudo where default methods could mistakenly point ... Read More

How to move a file from one folder to another using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 18:20:01

6K+ Views

The Python shutil module provides a number of functions for high-level operations on individual files and file collections. In this article, we will go through different methods of moving a file from one folder to another using Python. Using the OS Module The Python OS module gives users the ability ... Read More

How to remove swap files using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 18:12:01

793 Views

Swap files and temporary files are common byproducts of text editors such as Vim, Emacs or even modern IDEs. These files—often with extensions like .swp, .swo, .tmp or .bak are used to store session data or backup content temporarily. These are useful during editing sessions in which they can clutter ... Read More

Previous 1 ... 7 8 9 10 11 ... 19 Next
Advertisements