Found 26504 Articles for Server Side Programming

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

How to change file extension in Python?

Niharikaa Aitam
Updated on 15-May-2025 16:03:31

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

How to get specific nodes in xml file in Python?

SaiKrishna Tavva
Updated on 01-Sep-2025 14:55:03

10K+ Views

XML is abbrivated as Extensible Markup Language which is used format to represent a structured data. It's is useful when we are exchanging information between two systems. In Python, the xml.etree.ElementTree module helps us to read and work with XML data. In this article, we will explore how to extract specific parts of an XML file using this library. Introduction to XML and ElementTree When we are working with XML files in Python, the xml.etree.ElementTree module is used for parsing and navigating XML structures. It reads an XML document and builds a tree of elements from it by allowing us ... Read More

How to move a file from one folder to another using Python?

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

6K+ Views

The Python shutil module provides a number of functions for high-level operations on individual files and file collections. In this article, we will go through different methods of moving a file from one folder to another using Python. Using the OS Module The Python OS module gives users the ability to create interactions with their operating systems. Using shutil.move() method Following is an example which shows how to move a file from one folder to another using shutil.move() method - # importing the modules import shutil import os # Providing the folder path origin = 'C:\Users\Lenovo\Downloads\Works' target = 'C:\Users\Lenovo\Downloads\Work ... Read More

How to copy certain files from one folder to another using Python?

SaiKrishna Tavva
Updated on 01-Sep-2025 14:53:40

3K+ Views

When we are working with files in Python we may need to copy only a certain type of file. Python makes this task simple and flexible by using built-in modules such as os and shutil and pathlib. Copying specific files using shutil and os modules When we want to copy only certain files such as all .txt files from one folder to another in Python then we can use the shutil and os modules. The shutil module is used to perform high-level operations on files such as file copying, moving, archiving and removing directories whereas os module is used to ... Read More

How to copy files from one folder to another using Python?

Sarika Singh
Updated on 15-May-2025 17:52:03

45K+ Views

We can easily check the Odd or Even by using conditional statements. We can divide the number by 2, then check whether the remainder is 0 or not. If 0, then it is even. We can also perform the AND operation with the number and 1. If the answer is 0, then it is even; otherwise odd. There is no need to use conditional statements in both approaches. We will see two different methods to check the odd or even. Using Modulo Operator This approach uses the Modulo Operator ... Read More

Advertisements