We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

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 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: 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

Cart

Your Cart is Empty

Back To Shop