thispointer.com thispointer.com

Python- Find the largest file in a directory

In this article, we will discuss how to find the largest file in a directory and its sub-directories using python. Table of contents Find the largest file in a directory using python.Find the largest file in a directory and its sub-directories using python. Get the largest file in a directory using python In python, the glob module provides a function glob() to find files/directories in a given directory based on the matching pattern....

thispointer.com thispointer.com

Python: Get list of files in directory sorted by size

In this article, we will discuss different ways to get list of all files in a directory / folder sorted by size. Table of contents Get list of files (file paths) in directory sorted by size.Get list of file names in directory sorted by size.Get list of files (file paths) in directory and sub-directories sorted by size. Get list of files in directory sorted by size using glob() In python, the glob module provides a function glob() to...

thispointer.com thispointer.com

Python Get list of files in directory with size

In this article, we will discuss different ways to get list of all files in a directory or folder along with size in python. Table of contents Get list of files (file paths) in directory with size.Get list of file names in directory with size.Get list of files (file paths) in directory and sub-directories with size. Get list of all files in directory with size using glob() In python, the glob module provides a function glob() to find...

If you want to learn python, coming from a java paradigm and have more of java coding style in your blood! You have just landed on right page. Here a folder structure is followed to handle different use-cases one struggles through like how classes are cre
github

If you want to learn python, coming from a java paradigm and have more of java coding style in your blood! You have just landed on right page. Here a folder structure is followed to handle different use-cases one struggles through like how classes are cre

bharatmakhija/Python If you want to learn python, coming from a java paradigm and have more of java coding style in your blood! You have just landed on right page. Here a folder structure is followed to handle...

thispointer.com thispointer.com

Python: How to insert lines at the top of a file?

In this article, we will discuss how to insert single or multiple lines at the beginning of a text or CSV file in python. How to insert a line at the top of a file There is no direct way to insert text in the middle of a file. Therefore we have to create a new file with the new line at the top and then rename this file as the original file. We have created a function for that,import os def prepend_line(file_name, line): ...

thispointer.com thispointer.com

Python: Get last N lines of a text file, like tail command

In this article, we will discuss a memory-efficient solution to read the last line or last N lines from a text or CSV file in python. Then we will also see how to real only last line or check if the last line in the file matches the given line. We have created a function to read last N lines from a text file,import os def get_last_n_lines(file_name, N): # Create an empty list to keep the track of last N lines list_of_lines =...

thispointer.com thispointer.com

Python: How to append a new row to an existing csv file?

In this article, we will discuss how to append a row to an existing csv file using csv module’s reader / writer & DictReader / DictWriter classes. Suppose we have a CSV file students.csv, whose contents are,Id,Name,Course,City,Session 21,Mark,Python,London,Morning 22,John,Python,Tokyo,Evening 23,Sam,Python,Paris,MorningThere can be many ways in python, to append a new row at the end of this csv file. But here we will discuss...

thispointer.com thispointer.com

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 to delete lines at specific places in a file i.e., “Copy the contents of the given file to a temporary file line by line and while copying...

thispointer.com thispointer.com

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 the given path in bytes. If the file doesn’t exist at the given path or it is inaccessible, then it...

thispointer.com thispointer.com

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,os.stat(path, *, dir_fd=None, follow_symlinks=True)It accepts file path (string) as an argument and returns an object of the structure stat, which contains...

thispointer.com thispointer.com

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,def check_if_string_in_file(file_name, string_to_search): """ Check if any line in the file contains given string """ # Open...

thispointer.com thispointer.com

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 exists or not. If the file doesn’t exist, then it creates an empty file and opens it. Whereas, if the file already exists then it...

thispointer.com thispointer.com

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 i.e.ZipFile.namelist()It returns a list of file names in Zip archive. Let’s use this to get the list of files in a zip file...

thispointer.com thispointer.com

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,ZipFile.extractall(path=None, members=None, pwd=None)It accepts following arguments : path : location where zip file need to be extracted, if not provided it will extract...

thispointer.com thispointer.com

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.os.remove(path_of_file)It accepts the file path as argument and deletes the file at that path. File path can be relative to current working directory or an absolute path. For...