Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 648 of 2109
How do I do a case-insensitive string comparison in Python?
In Python, string comparisons are case-sensitive by default. For example, when we consider the strings "Hello" and "hello" then they are considered as two different strings because of the difference in their letter casing. Case-insensitive String Comparison in Python In some tasks, such as searching user input, comparing filenames, or processing natural language, it's important to compare strings without considering case differences. To perform string comparison in Python, we have several built-in methods such as lower(), upper(), and casefold(), which help us to normalize the casing of strings before comparing them. ...
Read MoreHow to search and replace text in a file using Python?
File manipulation is a fundamental aspect of programming, especially when dealing with data processing and management. Python offers powerful tools for handling files and text efficiently. A common task is searching for specific text patterns within a file and replacing them with desired content. This article explores several practical methods for searching and replacing text in a file using Python. Basic Text Replacement Let's start with a simple example of searching for a specific word in a file and replacing it with another word. We'll search for the word "old" and replace it with "new" ? ...
Read MoreWhat is the difference between Python\'s re.search and re.match?
In Python, Regular expressions (RegEx) are sequences of characters that define search patterns. The re module provides several functions to work with patterns in text for tasks like string matching, validation, and manipulation. The most commonly used functions are re.match() and re.search(). Both check for pattern presence but differ in where they look for matches within a string. The re.match() Method The re.match() method checks if the beginning of a string matches the specified pattern. It returns a match object if found at the start, otherwise returns None. Syntax re.match(pattern, string, flags=0) ...
Read MoreHow to perform different commands over ssh with Python?
When we want to remotely access and execute commands on another machine, we use the paramiko library in Python. Paramiko is a third-party library that enables secure communication with remote machines using the SSH (Secure Shell) protocol. It allows us to execute commands, transfer files, and perform other remote tasks programmatically. To use the Paramiko library, first we need to install it ? pip install paramiko Basic SSH Connection and Command Execution Here's how to connect to a remote server via SSH and execute multiple shell commands ? import paramiko # ...
Read MoreHow to copy a file to a remote server in Python using SCP or SSH?
When we want to transfer files from our local system to a remote server securely, Python provides several ways to do file transfer using the Paramiko and SCP libraries. These libraries support SSH-based file transfer, which is secure and reliable. Installing Required Libraries Before we start with file transfer, we need to install the required libraries ? pip install paramiko scp Understanding SCP and SSH Paramiko is a third-party Python library that provides SSH and SFTP functionality using the SSHv2 protocol. SCP stands for Secure Copy Protocol, which copies files between computers over ...
Read MoreHow to get the maximum file name length limit using Python?
In Python, when performing file operations such as creating or renaming files, one of the limitations is the maximum length limit of a file name. When we exceed this limit, it results in errors such as OSError: [Errno 36] File name too long. Using os.pathconf() Function In Python, the os.pathconf() function is used to get system-specific configuration values related to files and directories. This function helps in checking limits such as the maximum file name length or the maximum path length allowed by the file system. Syntax Following is the syntax of the os.pathconf() function − ...
Read MoreWhat is the maximum file size we can open using Python?
In Python, there is no fixed maximum file size that we can open. Python can open files of any size as long as the operating system and file system support it and there is enough memory and disk space available. The limit of maximum file size comes with the system architecture, i.e., 32-bit, 64-bit, the file system, such as FAT32, NTFS, ext4, and how the file is processed in the code. System and File System Limits Following are the system and file system limits ? FAT32 − Maximum file size: 4 ...
Read MoreHow to extract all the .txt files from a zip file using Python?
Python provides a built-in module called zipfile that allows us to create, read, write, and extract ZIP archives. When we want to extract only specific files, such as all .txt files, we can filter file names using string methods like endswith() or the pathlib module. A ZIP file is an archive format used to compress one or more files into a single file for easy storage and transfer. It reduces file size and keeps related files together for sharing over the internet and saving disk space. Steps for Extracting Specific Files Follow these steps to ...
Read MoreHow are files extracted from a tar file using Python?
A TAR file is abbreviated as Tape Archive, which is an archive format used mainly in Unix and Linux environments. The Tar file is used to collect multiple files into a single file, which makes it easier to share or backup together. In Python, when we want to work with TAR files, we can use the tarfile module, which allows us to create, read and extract TAR archives programmatically. In this article, we will explore how to extract files from a tar file using Python. Extracting All Files from a Tar File The extractall() method in ...
Read MoreHow are files added to a tar file using Python?
Python offers a built-in module named tarfile, which allows developers to create, read, and modify tar archives (standard Unix tarballs). We can use this module to compress multiple files into a single archive, with or without compression. File Modes for Creating Tar Archives Here are the available file modes to create a tar file using Python − "w": Write a tar archive without compression. "w:gz": Write a gzip-compressed archive. "w:bz2": Write a bzip2-compressed archive. "w:xz": Write an xz-compressed archive (Python 3.3+). Adding a Single File to ...
Read More