Server Side Programming Articles

Page 651 of 2109

How to create an empty file using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 25K+ 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', it creates a new file if it doesn't exist and overwrites the file if it already exists ? Example ...

Read More

How to find the mime type of a file in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 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 guesses the MIME type and encoding of a file based on its filename or URL. This method relies on file extensions and is part of Python's ...

Read More

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
Showing 6501–6510 of 21,090 articles
« Prev 1 649 650 651 652 653 2109 Next »
Advertisements