Niharikaa Aitam

Niharikaa Aitam

77 Articles Published

Articles by Niharikaa Aitam

Page 5 of 8

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

Niharikaa Aitam
Niharikaa Aitam
Updated on 27-May-2025 5K+ Views

In Python, we can extract a specific part of a file path of the directory using built-in modules such os.path and pathlib. Extracting a part of the file path is commonly needed when working with file systems, data processing, or scripts that handle files dynamically. Using os.path Module The os.path module in Python provides functions to manipulate files and directory paths. We can use this module to extract directory names, file names, and traverse up directory levels. Example Following is an example, which shows how to use the os.path module to extract a directory path from a full file path ...

Read More

How to list directory tree structure in python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 27-May-2025 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 all the possible methods in Python to list the directory tree structure. 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 through the tree in either a ...

Read More

How to open new pseudo-terminal pair using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 1K+ Views

Pseudo-terminals (pty) are an advanced and powerful technique in Python when we are dealing with terminal-based processes or simulating interactive sessions programmatically. A pseudo-terminal allows a process to connect to a real terminal. 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. Using pty.openpty() The pty module in Python provides the openpty() function that returns a tuple containing file descriptors for the master and slave ends of a new pseudo-terminal pair. Example Following is an example, which shows ...

Read More

How do we specify the buffer size when opening a file in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 9K+ Views

When we open a file using the Python's built-in function open(), then the data temporarily stored in memory can be managed by setting the buffering parameter in the function. Buffering helps to improve the performance of opening a file by reducing the number of interactions with the disk during file input/output operations. Understanding the Buffering Parameter The buffering parameter in Python's open() function allows us to define how much data is stored in memory before being written to or read from the file. This buffering parameter is used to handle file operations with large data or frequent writes. Syntax Here ...

Read More

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

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 1K+ Views

When the U modifier is used while opening a file then 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 find difference between 2 files in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 11K+ Views

In most of the applications, especially in data processing, software development or testing, it is required to compare two files to detect changes, validate outputs or find discrepancies. Python offers several ways to compare files which ranges from basic line-by-line comparisons to more advanced diff utilities. Following are the key methods which are used to compare two files in python - Line-by-line comparison: This is a straightforward approach to textual differences. difflib module: This module is used to produce human-readable diffs similar to Unix’s diff command. filecmp module: This module is used for quick binary or shallow comparisons. ...

Read More

How to share common data among multiple Python files?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 2K+ Views

In Python, sharing common data such as constants, configuration settings or shared resources among multiple files is a routine task in modular application development. The data will be centralized in a dedicated module which allows it to be maintained in one place and imported wherever needed. Before proceeding with various approaches to share data among multiple Python files, first we need to understand what is a module and what is shared data. What is a Module? A Module is a Python file (.py) that contains definitions such as variables, functions, classes or runnable code. By placing shared data in its ...

Read More

How to remove hidden files and folders using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 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 programatically 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. Here is an example of removing the hidden files and directories present in ...

Read More

How do you get a directory listing sorted by their name in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 22K+ Views

When we are dealing with large directories, managing files and folders is often required in listing them in a specific order. One common approach to list out the files/folders is sorting items by their names, which makes navigation and processing much easier. Python simplifies this task with built-in modules such as os and pathlib, which allow developers to fetch and organize directory contents with just a few lines of code. In this article, we will go through the different methods to retrieve and sort directory listings alphabetically with clean and flexible solutions for our file-handling needs. Using os.listdir() with sorted() ...

Read More

How to change file extension in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 15-May-2025 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 in Python is used to split 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 os.path.splitext() ...

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