Articles on Trending Technologies

Technical articles with clear explanations and examples

How to extract file extension using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 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.path.splitext() method of the os module in Python is used to split the file name into the name and extension. This method returns a tuple containing the filename without extension and the extension (including the dot). Example ...

Read More

How to change file extension in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 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 splits 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 ...

Read More

How to get specific nodes in xml file in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 10K+ Views

XML is abbreviated as Extensible Markup Language which is a format used to represent structured data. It's useful when exchanging information between systems. In Python, the xml.etree.ElementTree module helps us read and work with XML data. In this article, we will explore how to extract specific nodes from an XML file using this library. Introduction to XML and ElementTree When 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, allowing us to easily access and manipulate individual parts of ...

Read More

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

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 7K+ Views

The Python shutil module provides functions for high-level file operations. Python offers several methods to move files between directories, each with different advantages. Using shutil.move() Method The shutil.move() function moves files or directories from source to destination. It can handle both files and directories automatically ? import shutil import os # Moving a single file shutil.move('source_file.txt', 'destination_folder/') print("File moved successfully") File moved successfully Moving Multiple Files import shutil import os # Create sample files for demonstration with open('file1.txt', 'w') as f: f.write("Sample file ...

Read More

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

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 4K+ Views

When working with files in Python, we often need to copy only certain types of files from one folder to another. Python provides several built-in modules like os, shutil, and pathlib to make this task simple and flexible. Using shutil and os Modules The shutil module performs high-level file operations like copying, moving, and archiving, while the os module handles operating system interactions for file and directory manipulation. Copying Files by Extension Here's how to copy all .txt files from a source folder to a destination folder ? import os import shutil # ...

Read More

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

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 46K+ Views

Python provides several modules to copy files between folders. The most commonly used are shutil, os, and pathlib. Each offers different methods depending on whether you want to copy individual files or entire directories. Using shutil.copy() The shutil.copy() function copies a file from source to destination, preserving file permissions but not metadata ? import shutil import os # Create source directory and file for demonstration os.makedirs('source_folder', exist_ok=True) with open('source_folder/sample.txt', 'w') as f: f.write('Hello, this is a sample file!') # Create destination directory os.makedirs('destination_folder', exist_ok=True) # Copy file from source ...

Read More

How to compare two different files line by line in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 12K+ Views

Comparing two files line by line is a common task in Python programming. This tutorial explores different methods to compare files, from basic line-by-line comparison to using specialized modules like filecmp and difflib. Basic Line-by-Line Comparison The simplest approach uses the open() function to read both files and compare them manually. This method gives you full control over the comparison logic. Example Here's how to compare two files and identify differences − # Create sample files for demonstration with open('file1.txt', 'w') as f1: f1.write("Line 1Line 2Line 3Line 4") with ...

Read More

How to touch all the files recursively using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 1K+ 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 or bottom-up. ...

Read More

How to zip a folder recursively using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 7K+ Views

Zipping a folder recursively means compressing a folder along with all its subfolders and files into a single archive. Python provides several methods to accomplish this task efficiently. Using zipfile and os.walk() The zipfile module combined with os.walk() provides manual control over the compression process. This approach allows you to customize how files are added to the archive ? import zipfile import os def zip_folder_manual(folder_path, zip_path): with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(folder_path): ...

Read More

How to install a Python package into a different directory using pip?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 22K+ Views

In Python, pip is a standard tool used to install third-party packages. By default, pip installs packages into the site-packages directory of the current Python environment. However, in some cases, such as restricted environments or creating portable applications, you may need to install packages into a different directory using pip's --target option. Syntax The basic syntax for installing a Python package in a custom directory is − pip install --target This command tells pip to install the specified package and all its dependencies in the given directory. In this article, we'll ...

Read More
Showing 7691–7700 of 61,297 articles
« Prev 1 768 769 770 771 772 6130 Next »
Advertisements