Found 10478 Articles for Python

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

SaiKrishna Tavva
Updated on 20-Mar-2025 13:56:18

7K+ 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
Updated on 15-May-2025 18:23:11

3K+ Views

Monitoring file access times is a common requirement for auditing, data management and cleanup of the 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 In Python, we can use the os.path.getatime() method to retrieve a path's most recent access time. The path that we need to verify for the access time is taken as the input argument by os.path.getatime() method. This method returns the amount of time since the epoch, as a floating point value. It throws one OSError if the requested path cannot be ... Read More

How to remove swap files using Python?

Sarika Singh
Updated on 15-May-2025 18:12:01

737 Views

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

How to remove hidden files and folders using Python?

Niharikaa Aitam
Updated on 15-May-2025 18:04:57

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

Sarika Singh
Updated on 15-May-2025 15:26:41

1K+ Views

Listing files and directories using Python is a common task in many applications, from file management tools to automated scripts. However, when we are working on the Windows Operating System, the 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) whereas Windows uses metadata to mark files as hidden. In this article, we'll explore several methods to list only non-hidden files and directories. Basic Filtering by Name Prefix This is the basic Filtering method, which checks ... Read More

How can I list the contents of a directory in Python?

Sarika Singh
Updated on 17-Aug-2022 13:20:42

5K+ Views

A computer's file system's directory is an organisational feature used to store and locate files. A tree of directories is created by organising directories hierarchically. There are parent-child relationships in directories. A folder can also be used to refer to a directory. Python has accumulated a number of APIs that can list the directory contents over time. Useful functions include Path.iterdir, os.scandir, os.walk, Path.rglob, and os.listdir. We could need a list of files or folders in a given directory in Python. You can accomplish this in a number of ways. OS module A function that returns a list of the ... Read More

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

SaiKrishna Tavva
Updated on 17-Mar-2025 17:01:40

6K+ Views

When working with directories in Python, hidden files can often create clutter. Hidden files typically start with a dot (.) on Unix-like operating systems, and you may want to filter them out to ensure cleaner handling of your files. Below are four effective methods to ignore hidden files using the os module and pathlib. Each method includes samples, explanations, and expected outputs. Using List Comprehension with 'os.listdir()' One of the simplest and most efficient ways to ignore hidden files when listing a directory's contents is to use list comprehension. List comprehension allows us to filter out specific items based on ... Read More

How to open new pseudo-terminal pair using Python?

Niharikaa Aitam
Updated on 15-May-2025 18:38:47

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

Sarika Singh
Updated on 15-May-2025 18:08:07

24K+ 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', then it creates a new file if it doesn't exist. Example Following is an example, which shows how to create an empty file using ... Read More

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

Niharikaa Aitam
Updated on 15-May-2025 15:48:28

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 is used to guess the MIME type and to encode of a file based on its filename or URL. Example In this example, we are using the mimetypes.guess_type() ... Read More

Advertisements