Tag: If
Python : How to remove a file if exists and handle errors | os.remove() | os.ulink()
In this article we will discuss how to remove a file if only it exists and how to handle other types of exceptions using os.remove() & os.ulink(). How to remove a file using os.remove() python ‘s os module provides a function to remove the file i.e. Advertisements os.remove(path_of_file) It accepts the file path as argument …
Python : How to remove a file if exists and handle errors | os.remove() | os.ulink()Read More
Python : Check if a process is running by name and find it’s Process ID (PID)
In this article we will discuss a cross platform way to find a running process PIDs by name using psutil. To install python’s psutil library use, Advertisements pip install psutil Check if a process is running To check if process is running or not, let’s iterate over all the running process using psutil.process_iter() and match …
Python : Check if a process is running by name and find it’s Process ID (PID)Read More
Check if a Pandas DataFrame is empty or not
In this article, we will discuss four different ways to check if a given DataFrame is empty or not. Check whether DataFrame is empty using Dataframe.empty In Python’s pandas, the Dataframe class provides an attribute empty i.e. Dataframe.empty It return True if DatafFrame contains no data. Advertisements Let’s see an example, Create an empty DatafFrame …
Python : Check if a String contains a sub string & find it’s index | case insensitive
In this article we will discuss different ways to check if a sub-string exists in another string and if yes then find it’s index. Will also explore ways to check the existence of sub-string in case in-sensitive manner by ignoring case. Check if a String contains another string or character using “in” and “not in” …
Python : Check if a String contains a sub string & find it’s index | case insensitiveRead More
Python : How to use if, else & elif in Lambda Functions
In this article we will discuss how to use if , else if and else in a lambda functions in Python. Will also explain how to use conditional lambda function with filter() in python. Using if else in Lambda function Using if else in lambda function is little tricky, the syntax is as follows, lambda …
Python : How to use if, else & elif in Lambda FunctionsRead More
Pandas : Check if a value exists in a DataFrame using in & not in operator | isin()
In this article we will dicuss different ways to check if a given value exists in the dataframe or not. First of all, we need to import the pandas module i.e. Advertisements import pandas as pd Let’s create a dataframe, # List of Tuples empoyees = [(‘jack’, 34, ‘Sydney’, 155) , (‘Riti’, 31, ‘Delhi’ , …
Pandas : Check if a value exists in a DataFrame using in & not in operator | isin()Read More
Python: Three ways to check if a file is empty
In this article, we will discuss different ways to check if a file is empty i.e. its size is 0 using os.stat() or os.path.getsize() or by reading its first character. Check if a file is empty using os.stat() in Python Python provides a function to get the statistics about the file, Advertisements os.stat(path, *, dir_fd=None, …
Python: Check if a List is empty
In this article, we will discuss different ways to check if a list is empty or not. We will also see how to check if a list of lists is empty or not using for loop, List comprehension, and all() function.Table of Contents Check if a list is empty using ‘not’ operator in python Check …
Check if all elements in a list are None in Python
In this article we will discuss different ways to check if a list contains only None or not. Suppose we have a list, Advertisements sample_list = [None, None, None, None] Now we want to confirm that all items in this list are None. There are different ways to do that. Let’s discuss them one by …
6 Ways to check if all values in Numpy Array are zero (in both 1D & 2D arrays) – Python
In this article we will discuss seven different ways to check if all values in a numpy array are 0. Then we will look how to find rows or columns with only zeros in a 2D array or matrix. Check if all values in a 1D Numpy Array are Zero First of all, we will …
6 Ways to check if all values in Numpy Array are zero (in both 1D & 2D arrays) – PythonRead More