Category: FileHandling
Python : How to delete a directory recursively using shutil.rmtree()
In this article we will discuss how to delete an empty directory and also all contents of directory recursively i.e including contents of its sub directories. Delete an empty directory using os.rmdir() Python’s os module provide a function to delete an empty directory i.e. Advertisements os.rmdir(pathOfDir) Path of directory can be relative or absolute. It …
Python : How to delete a directory recursively using shutil.rmtree()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
Python : How to get the list of all files in a zip archive
In this article we will discuss different ways to get detail about all files in a zip archive like file’s name & size etc. Get the name of all files in the ZIP archive using ZipFile.namelist() In Python’s zipfile module, ZipFile class provides a member function to get the names of all files in it …
Python : How to get the list of all files in a zip archiveRead More
Python: How to create a zip archive from multiple files or Directory
In this article we will discuss how to create a zip archive from selected files or files from a directory based on filters. Python’s zipfile module provides a ZipFile class for zip file related stuff. Let’s use this to create a zip archive file. Advertisements First import the class from module i.e. from zipfile import …
Python: How to create a zip archive from multiple files or DirectoryRead 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
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, …
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: How to delete specific lines in a file in a memory-efficient way?
In this article, we will discuss different ways to delete specific lines from a file either by line number, by matching content or based on any custom logic. In Python, there is no direct API to delete lines or text from the middle of a file. Therefore, in this article, we will follow an approach …
Python: How to delete specific lines in a file in a memory-efficient way?Read More