Tag: File
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: How to unzip a file | Extract Single, multiple or all files from a ZIP archive
In this article we will discuss different ways to unzip or extract single, multiple or all files from zip archive to current or different directory. In Python’s zipfile module, ZipFile class provides a member function to extract all the contents from a ZIP archive, Advertisements ZipFile.extractall(path=None, members=None, pwd=None) It accepts following arguments : path : …
Python: How to unzip a file | Extract Single, multiple or all files from a ZIP archiveRead More
Pandas : skip rows while reading csv file to a Dataframe using read_csv() in Python
In this article we will discuss how to skip rows from top , bottom or at specific indicies while reading a csv file and loading contents to a Dataframe. Python panda’s library provides a function to read a csv file and load data to dataframe directly also skip specified lines from csv file i.e. Advertisements …
Pandas : skip rows while reading csv file to a Dataframe using read_csv() in PythonRead More
Pandas : Read csv file to Dataframe with custom delimiter in Python
In this article we will discuss how to read a CSV file with different type of delimiters to a Dataframe. Python’s Pandas library provides a function to load a csv file to a Dataframe i.e. Advertisements pandas.read_csv(filepath_or_buffer, sep=’, ‘, delimiter=None, header=’infer’, names=None, index_col=None, ….) It reads the content of a csv file at given path, …
Pandas : Read csv file to Dataframe with custom delimiter in PythonRead More
How to save Numpy Array to a CSV File using numpy.savetxt() in Python
In this article we will discuss how to save 1D & 2D Numpy arrays in a CSV file with or without header and footer. numpy.savetxt() Python’s Numpy module provides a function to save numpy array to a txt file with custom delimiters and other custom options i.e. Advertisements numpy.savetxt(fname, arr, fmt=’%.18e’, delimiter=’ ‘, newline=’n’, header=”, …
How to save Numpy Array to a CSV File using numpy.savetxt() in PythonRead More
Python: Open a file using “open with” statement & benefits explained with examples
In this article, we will discuss how to use “open with” statement to open a file and what are its benefits. Also, how to open multiple files in a single “open with” statement. The need for “open with” statement Before going into the “with statement” we need to understand the requirement behind it. For that, …
Python: Open a file using “open with” statement & benefits explained with examplesRead More
How to append text or lines to a file in python?
In this article, we will discuss how to append text or new lines to an existing file using python. To append some text to a file in the end, we first need to open the file with access mode ‘a’, file_object = open(‘sample.txt’, ‘a’) With file access mode ‘a’, open() function first checks if file …
Python: Search strings in a file and get line numbers of lines containing the string
In this article, we are going to discuss, how to search for single or multiple strings in a file and get all the matched lines along with their line numbers. Check if a string exists in a file To check if a given string exists in the file or not, we have created a function, …
Python: Search strings in a file and get line numbers of lines containing the stringRead More
Python: Get file size in KB, MB or GB – human-readable format
In this article, we will discuss different ways to get file size in human-readable formats like Bytes, Kilobytes (KB), MegaBytes (MB), GigaBytes(GB) etc. Different ways to get file size in Bytes Get file size in bytes using os.path.getsize() os.path.getsize(path) It accepts the file path as an argument and returns the size of a file at …
Python: Get file size in KB, MB or GB – human-readable formatRead 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, …