
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 10476 Articles for Python

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

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

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

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

11K+ Views
This tutorial looks at various Python comparison techniques for two files. We'll go over how to perform this typical work by using the available modules, reading two files, and comparing them line by line. In Python, comparing two files can be done in a variety of ways. Comparing Two Text Files Line by Line Using the open() function to read the data from two text files, we can compare the information they contain. The local directory will be searched for and potentially read by the open() function. Example We'll contrast two files containing python data in this example. We're informed ... Read More

17K+ Views
When working with the Python scripts, it is required to interact with user-specific files or directories and also it's essential to accurately determine the actual user's home directory. This is especially important if the script might be executed with elevated privileges like using sudo where default methods could mistakenly point to /root directory instead of the true user's home. Python offers multiple ways to retrieve the home directory and selecting the appropriate method which helps to ensure consistent and expected behavior across different environments and permission levels. In this articlE, we will go through the different methods to find the ... Read More

947 Views
In file system management, it's sometimes necessary to update the modification or access time of files which is commonly referred as "touching" files. This is useful in automation scripts, build systems or cache invalidation mechanisms. Python offers powerful modules such as os and pathlib to touch all files within a directory recursively. In this article, we'll explore different methods to recursively touch files using Python by ensuring each file's timestamp is refreshed as needed. Using os.walk() and os.utime() The os.walk() function generates the file names in a directory tree by walking the tree either top-down approach or bottom-up approach. It ... Read More

7K+ Views
Zipping a folder recursively means compressing a folder along with all its subfolders and files. In this article, we will explore all the possible ways to zip a folder recursively using Python. Using zipfile and os.walk() In Python, we have the methods zipfile() and os.walk() to zip all the folders recursively. This is a manual approach of zipping the available subfolders and files into one. Following is an example that shows how to zip a folder recursively using Python - import zipfile import os def zip_folder_manual(folder_path, zip_path): with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: ... Read More

20K+ Views
In Python, pip is a standard tool which is used to install third-party packages in our system. By default, pip installs packages into the site-packages directory of the current Python environment but in some cases, such as restricted environments or creating portable apps, we may need to install packages into a different directory. Then we can use the pip's --target option. Syntax Following is the syntax of using the pip command while installing a python package in a different directory - pip install --target The above command tells pip to install the specified package and all its ... Read More