Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 661 of 2547
How do we specify the buffer size when opening a file in Python?
When opening a file using Python's built-in open() function, the data temporarily stored in memory can be managed by setting the buffering parameter. Buffering helps improve file I/O performance by reducing the number of disk interactions during read/write 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 parameter is essential when handling large files or frequent write operations ? Syntax open(file, mode='r', buffering=-1, encoding=None, ...) Buffering Modes The ...
Read MoreWhat does the \'U\' modifier do when a file is opened using Python?
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 MoreWhat does the \'b\' modifier do when a file is opened using Python?
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 MoreWhat are the modes a file can be opened using Python?
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 MoreHow to check file last access time using Python?
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 MoreHow to remove swap files using Python?
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 MoreHow to remove hidden files and folders using Python?
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 MoreHow to list non-hidden files and directories in windows using Python?
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 MoreHow to ignore hidden files using os.listdir() in Python?
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 MoreHow to open new pseudo-terminal pair using Python?
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