Niharikaa Aitam

Niharikaa Aitam

77 Articles Published

Articles by Niharikaa Aitam

Page 5 of 8

What does the \'U\' modifier do when a file is opened using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 1K+ Views

When the U modifier is used while opening a file, Python opens the file in Universal Newline mode. This mode enables Python to automatically detect and handle all common newline characters including , \r and \r during file reading. It is particularly useful when working with text files generated on various operating systems such as Windows, macOS or Linux which use different newline conventions. The U mode was used in Python 2 and early Python 3 versions to enable newline normalization. It allowed consistent handling of line endings regardless of the platform on which the file was created. However, ...

Read More

How to remove hidden files and folders using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 4K+ Views

Managing hidden files and folders in a directory is an essential part of building cleanup scripts or automation tools. On Windows, hidden files are not prefixed with a dot but instead they are marked using specific file attributes. In this article, we will explore how to remove those hidden files and directories programmatically using Python. Filtering by Dot Prefix (Unix/Linux/macOS) On Unix-based systems, the files or folders starting with a dot "." are considered hidden. We can use this naming pattern to filter and remove hidden files ? import os import shutil def remove_hidden_unix(path): ...

Read More

How to open new pseudo-terminal pair using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 2K+ Views

Pseudo-terminals (pty) are an advanced and powerful technique in Python when dealing with terminal-based processes or simulating interactive sessions programmatically. A pseudo-terminal allows a process to connect to a real terminal, enabling control over terminal input and output. Python provides a built-in pty module that helps in creating and managing these terminal pairs. In this article, we will learn how to open a new pseudo-terminal pair using Python. Understanding Pseudo-terminals A pseudo-terminal consists of two parts: a master and a slave. The master end is controlled by your Python program, while the slave end behaves like a ...

Read More

How to find the mime type of a file in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 8K+ Views

In some scenarios, it is important to determine the MIME type of a file to verify its content type, especially when working with file uploads or handling media files. Python provides modules such as mimetypes and magic to determine the MIME type of a file. In this article, we'll explore different methods to find a file's MIME type using Python. Using mimetypes.guess_type() The mimetypes module in Python provides the guess_type() function, which guesses the MIME type and encoding of a file based on its filename or URL. This method relies on file extensions and is part of Python's ...

Read More

How to change file extension in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 16K+ Views

In few scenarios, we need to change the extension of a file programmatically such as renaming a .txt file to .md or converting .csv to .json. Python provides different ways to do this using the os and pathlib modules. In this article, we'll explore how to change a file's extension using both approaches. Using os.path.splitext() The os.path.splitext() method of the os module splits the file name into the name and extension. We can use this method to strip off the old extension and add the new extension ? Example In this example, we are using the ...

Read More

How to touch all the files recursively using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 1K+ Views

In file system management, it's sometimes necessary to update the modification or access time of files which is commonly referred as "touching" files. This is useful in automation scripts, build systems or cache invalidation mechanisms. Python offers powerful modules such as os and pathlib to touch all files within a directory recursively. In this article, we'll explore different methods to recursively touch files using Python by ensuring each file's timestamp is refreshed as needed. Using os.walk() and os.utime() The os.walk() function generates the file names in a directory tree by walking the tree either top-down or bottom-up. ...

Read More

How to zip a folder recursively using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 7K+ Views

Zipping a folder recursively means compressing a folder along with all its subfolders and files into a single archive. Python provides several methods to accomplish this task efficiently. Using zipfile and os.walk() The zipfile module combined with os.walk() provides manual control over the compression process. This approach allows you to customize how files are added to the archive ? import zipfile import os def zip_folder_manual(folder_path, zip_path): with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(folder_path): ...

Read More

How to extract a part of the file path (a directory) in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 5K+ Views

In Python, we can extract specific parts of a file path using built-in modules such as os.path and pathlib. Extracting parts of file paths is commonly needed when working with file systems, data processing, or scripts that handle files dynamically. Using os.path Module The os.path module provides functions to manipulate files and directory paths. We can use this module to extract directory names, file names, and traverse up directory levels. Basic Directory Extraction The following example shows how to use os.path.dirname() to extract the parent directory from a file path − import os ...

Read More

How to list directory tree structure in python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 9K+ Views

The given task is to list the directory tree structure, i.e., we need to print the hierarchy of folders and files starting from a specified root directory. This is similar to how the tree command works in Linux or Windows by showing nested folders and files in a structured and indented format. In this article, we will see different methods in Python to list the directory tree structure using os.walk(), pathlib, and os.listdir(). Using os.walk() Method The os.walk() method in Python is used to generate the file names and folder names in a directory tree by parsing ...

Read More

How to check if a given directory contains any other directory in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 5K+ Views

In Python, when working with directory structures, it is necessary to check whether a given directory contains any other directories within it. This process is useful when performing batch operations, cleaning up folders, or traversing file systems. Python provides several built-in ways to perform this check effectively. In this article, we will explore different methods using both os and pathlib modules to determine if a directory contains any subdirectories. Using os.listdir() and os.path.isdir() The os.listdir() method lists directory contents, and when combined with os.path.isdir(), we can verify if an item is a directory. Example This ...

Read More
Showing 41–50 of 77 articles
« Prev 1 3 4 5 6 7 8 Next »
Advertisements