Articles on Trending Technologies

Technical articles with clear explanations and examples

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

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 14K+ Views

When we use the b modifier while opening a file, the file is opened in binary mode. Any file whose format doesn't consist of readable characters is referred to as a "binary" file. Binary files include audio files like MP3s, text formats such as Word or PDF, and image files such as JPEGs or GIFs. Normally, files are automatically opened in text mode in Python. When choosing a mode, include the letter "b" for binary mode. By default, the open() function opens a file in text format. As a result, the wb mode opens the file in binary format ...

Read More

What are the modes a file can be opened using Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 8K+ Views

When working with files in Python, it's crucial to understand the different modes in which files can be opened. Each mode defines specific operations you can perform − whether reading, writing, appending, or handling binary data. Following are the common file modes in Python. Read Mode: 'r' Write Mode: 'w' Binary Mode: 'b' Append Mode: 'a' Read Mode: 'r' The default mode for opening files in Python is read mode ('r'). This allows you to read the contents of a file without modifying ...

Read More

How to check file last access time using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 3K+ Views

Monitoring file access times is a common requirement for auditing, data management and cleanup scripts. Python provides multiple ways to retrieve the last access time of a file using the os and pathlib modules. Using os.path.getatime() Method The os.path.getatime() method retrieves a file's most recent access time. It takes the file path as input and returns the time since epoch as a floating-point number. Syntax os.path.getatime(path) Example 1: Basic Usage Here's how to check a file's last access time using os.path.getatime() ? import os import datetime # Create a ...

Read More

How to remove swap files using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 917 Views

Swap files and temporary files are common byproducts of text editors such as Vim, Emacs or modern IDEs. These files—often with extensions like .swp, .swo, .tmp or .bak—are used to store session data or backup content temporarily. While useful during editing sessions, they can clutter project directories or interfere with version control systems if not cleaned up. In this article, we explore how to leverage Python to automatically find and delete these unwanted swap files from our workspace. Using os.walk() for Recursive Deletion The os.walk() method recursively traverses directories and removes files that match common swap file ...

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 list non-hidden files and directories in windows using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 2K+ Views

Listing files and directories using Python is a common task in many applications, from file management tools to automated scripts. However, when working on the Windows Operating System, hidden files and folders are marked using specific file attributes, which can clutter the output if not handled properly. Unlike Unix-based systems, where hidden files typically start with a dot (such as .hidden), Windows uses metadata to mark files as hidden. In this article, we'll explore several methods to list only non-hidden files and directories on Windows systems. Basic Filtering by Name Prefix This basic filtering method checks if ...

Read More

How to ignore hidden files using os.listdir() in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 7K+ Views

When working with directories in Python, hidden files can clutter your file listings. Hidden files start with a dot (.) on Unix-like operating systems and can be easily filtered out using various Python techniques. In this article, we will explore different methods to ignore hidden files using the os module and pathlib. Using List Comprehension with os.listdir() The os.listdir() method includes hidden files (those starting with a dot) in its results. To filter out hidden files and list only visible files, we can use list comprehension with a condition ? import os # List ...

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 create an empty file using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 25K+ Views

Creating an empty file is a common task in programming when we are initializing log files, setting up placeholders or working with automation scripts. Python provides several easy and flexible methods to create empty files. In this article, we'll go through different ways to create an empty file in Python. Using open() with Write Mode 'w' The open() function is the most straightforward way to create an empty file. When we use the write mode 'w', it creates a new file if it doesn't exist and overwrites the file if it already exists ? Example ...

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
Showing 7681–7690 of 61,297 articles
« Prev 1 767 768 769 770 771 6130 Next »
Advertisements