Found 10476 Articles for Python

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

793 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

2K+ 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

6K+ 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 01-Sep-2025 14:59:22

6K+ Views

When we are working with directories in Python, a clutter of hidden files can be created. Hidden files are created with a dot (.) on Unix-like operating systems and we can filter them easily. In this article, we will explore different methods to ignore hidden files using the os module and pathlib. Using List Comprehension with os.listdir() In Python, when we use the os.listdir() method the hidden files i.e., the files those are starting with a dot . are also included in the result. To ignore those hidden files and list only visible files we need to pass a condition ... Read More

How to open new pseudo-terminal pair using Python?

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

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 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

How to extract file extension using Python?

Sarika Singh
Updated on 07-May-2025 14:01:01

6K+ Views

In a few scenarios, we need to extract the extension of a file to perform specific operations based on its type, such as validating image formats or filtering document files. Python provides different ways to achieve this using the os and pathlib modules. In this article, we'll explore how to get a file’s extension with different approaches.  Using os.path.splitext() The OS file path manipulation is made simple with the help of the Python os.path module. It provides methods to perform operations to receive data from file paths, opening, saving, and updating. The os.path.splitext() method of the os module in Python ... Read More

Advertisements