Sarika Singh has Published 189 Articles

How to create an empty file using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 18:08:07

24K+ Views

Creating an empty file is a common task in programming when we are initializing log files, setting up placeholders or working with automation scripts. Python provides several easy and flexible methods to create empty files. In this article, we'll go through different ways to create an empty file in Python. ... Read More

How to copy files from one folder to another using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 17:52:03

45K+ Views

We can easily check the Odd or Even by using conditional statements. We can divide the number by 2, then check whether the remainder is 0 or not. If 0, then it is even. We can also perform the AND operation ... Read More

How to know/change current directory in Python shell?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 17:51:53

40K+ Views

A portable method of interacting with the operating system is offered through the OS Python Module. This module is a part of the default Python library, which contains tools for locating and modifying the working directory. When we are working in the Python shell or scripting environment, it's important to ... Read More

How to list non-hidden files and directories in windows using Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 15:26:41

2K+ Views

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. ... Read More

What are the allowed characters in Python function names?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 14:20:17

2K+ Views

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 ... Read More

What is the difference between \'except Exception as e\' and \'except Exception, e\' in Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 14:07:20

1K+ Views

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 ... Read More

What is the use of \"assert\" statement in Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 13:57:00

371 Views

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 ... Read More

How to catch KeyError Exception in Python?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 13:54:35

953 Views

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 ... Read More

Explain try, except and finally statements in Python.

Sarika Singh

Sarika Singh

Updated on 15-May-2025 13:07:58

502 Views

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 ... Read More

How to return a json object from a Python function?

Sarika Singh

Sarika Singh

Updated on 15-May-2025 13:02:34

2K+ Views

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 ... Read More

Advertisements