Sarika Singh

Sarika Singh

142 Articles Published

Articles by Sarika Singh

Page 5 of 15

How to print characters from a string starting from 3rd to 5th in Python?

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

Python strings use indexing to access individual characters, starting from index 0. To extract characters from the 3rd to 5th position, we can use string slicing with different approaches. String Indexing Basics In Python, string indexing starts from 0. For example, the string "Coding" has indices 0, 1, 2, 3, 4, 5 for characters C, o, d, i, n, g respectively. text = "Coding" print(f"Length: {len(text)}") print(f"First character (index 0): {text[0]}") print(f"Third character (index 2): {text[2]}") Length: 6 First character (index 0): C Third character (index 2): d Method 1: Using ...

Read More

How to print all the keys of a dictionary in Python

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

A Python dictionary is an unordered collection of data values that stores key-value pairs. In this article, we will explore various methods to print all the keys of a dictionary in Python. Using dict.keys() Method The dict.keys() method returns a dictionary view object containing all the keys. This is the most direct way to access all keys in a dictionary ? Example dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } ...

Read More

What does the \'b\' modifier do when a file is opened using Python?

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

When we use the b modifier while opening a file, the file is opened in binary mode. Any file whose format doesn't consist of readable characters is referred to as a "binary" file. Binary files include audio files like MP3s, text formats such as Word or PDF, and image files such as JPEGs or GIFs. Normally, files are automatically opened in text mode in Python. When choosing a mode, include the letter "b" for binary mode. By default, the open() function opens a file in text format. As a result, the wb mode opens the file in binary format ...

Read More

How to check file last access time using Python?

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

Monitoring file access times is a common requirement for auditing, data management and cleanup scripts. Python provides multiple ways to retrieve the last access time of a file using the os and pathlib modules. Using os.path.getatime() Method The os.path.getatime() method retrieves a file's most recent access time. It takes the file path as input and returns the time since epoch as a floating-point number. Syntax os.path.getatime(path) Example 1: Basic Usage Here's how to check a file's last access time using os.path.getatime() ? import os import datetime # Create a ...

Read More

How to remove swap files using Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 916 Views

Swap files and temporary files are common byproducts of text editors such as Vim, Emacs or modern IDEs. These files—often with extensions like .swp, .swo, .tmp or .bak—are used to store session data or backup content temporarily. While useful during editing sessions, they can clutter project directories or interfere with version control systems if not cleaned up. In this article, we explore how to leverage Python to automatically find and delete these unwanted swap files from our workspace. Using os.walk() for Recursive Deletion The os.walk() method recursively traverses directories and removes files that match common swap file ...

Read More

How to list non-hidden files and directories in windows using Python?

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

Listing files and directories using Python is a common task in many applications, from file management tools to automated scripts. However, when working on the Windows Operating System, hidden files and folders are marked using specific file attributes, which can clutter the output if not handled properly. Unlike Unix-based systems, where hidden files typically start with a dot (such as .hidden), Windows uses metadata to mark files as hidden. In this article, we'll explore several methods to list only non-hidden files and directories on Windows systems. Basic Filtering by Name Prefix This basic filtering method checks if ...

Read More

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 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 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 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
Showing 41–50 of 142 articles
« Prev 1 3 4 5 6 7 15 Next »
Advertisements