
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10479 Articles for Python

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

935 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

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

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

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

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

9K+ Views
XML (Extensible Markup Language) is a widely used format for storing and transporting structured data. In Python, the `xml.etree.ElementTree` library provides a straightforward and efficient way to parse and manipulate XML data. This guide will walk you through extracting specific nodes from an XML file using ElementTree. Introduction to XML and ElementTree Python's ElementTree library allows us to parse an XML file into an element tree, where each element corresponds to a node in the tree. With ElementTree, we can traverse this tree to find and extract specific nodes based on various criteria. Parsing an XML File To begin, we ... Read More

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

3K+ Views
Python offers various tools for file manipulation such as shutil module (short for shell utility) which allows us to manipulate files and directories, enabling you to perform various file and directory operations. Copying Files From one Folder to Another The goal is to scan a source folder, identify files based on specific criteria, and duplicate them into a target folder. We will utilize Python's standard library modules: 'Os', 'shutil', and 'glob' Os: Interacts with the operating system for file system navigation and path management. shutil: Provides functions for high-level file operations ... Read More

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