Articles on Trending Technologies

Technical articles with clear explanations and examples

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

How to extract a part of the file path (a directory) in Python?

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

In Python, we can extract specific parts of a file path using built-in modules such as os.path and pathlib. Extracting parts of file paths is commonly needed when working with file systems, data processing, or scripts that handle files dynamically. Using os.path Module The os.path module provides functions to manipulate files and directory paths. We can use this module to extract directory names, file names, and traverse up directory levels. Basic Directory Extraction The following example shows how to use os.path.dirname() to extract the parent directory from a file path − import os ...

Read More

How can I iterate over files in a given directory in Python?

Alekhya Nagulavancha
Alekhya Nagulavancha
Updated on 24-Mar-2026 11K+ Views

Iterating over files in a given directory helps to perform tasks such as finding files that match certain criteria or counting the number of files in a directory. Python provides several methods to walk through all the existing files in a directory ? os.listdir() method os.walk() method os.scandir() method Using pathlib module glob.iglob() method In this article, we will explore all these methods for iterating over files in a given directory in Python. Using os.listdir() Method The os.listdir() method ...

Read More

How to list directory tree structure in python?

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

The given task is to list the directory tree structure, i.e., we need to print the hierarchy of folders and files starting from a specified root directory. This is similar to how the tree command works in Linux or Windows by showing nested folders and files in a structured and indented format. In this article, we will see different methods in Python to list the directory tree structure using os.walk(), pathlib, and os.listdir(). Using os.walk() Method The os.walk() method in Python is used to generate the file names and folder names in a directory tree by parsing ...

Read More

How to check if a given directory contains any other directory in Python?

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

In Python, when working with directory structures, it is necessary to check whether a given directory contains any other directories within it. This process is useful when performing batch operations, cleaning up folders, or traversing file systems. Python provides several built-in ways to perform this check effectively. In this article, we will explore different methods using both os and pathlib modules to determine if a directory contains any subdirectories. Using os.listdir() and os.path.isdir() The os.listdir() method lists directory contents, and when combined with os.path.isdir(), we can verify if an item is a directory. Example This ...

Read More
Showing 1–10 of 61,302 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements