Convert Timestamp String to Datetime Object in Python

SaiKrishna Tavva
Updated on 15-May-2025 18:49:39

23K+ Views

In many real-world applications, timestamps are used to represent dates and times, but they are not human-readable. To make them understandable or use them in various datetime manipulations, it’s essential to convert them into Python’s datetime object. Python’s datetime module provides multiple functions to convert timestamps to datetime objects. Below are the various methods to accomplish this task - Using datetime.fromtimestamp() Function Using datetime.fromtimestamp() & strftime() Using datetime.strptime() Function Parsing Mixed Text Using strptime() Function Using datetime.fromtimestamp() Function To obtain a date ... Read More

Find Median of Elements from Two Different Arrays in C++

Aman Kumar
Updated on 15-May-2025 18:47:19

206 Views

The median is defined as the middle value of a sorted list of numbers, and the middle value is found by ordering the numbers in ascending order. Once the numbers are ordered, the middle value is called the median of the given data set. Here, in this article, we have two different sorted arrays and need to find the median of the array that is formed after merging of two given arrays. Median depends on the sorted merged array. So, the following cases may occur: If the length of the merged array is odd, then ... Read More

Open New Pseudo Terminal Pair Using Python

Niharikaa Aitam
Updated on 15-May-2025 18:38:47

1K+ Views

Pseudo-terminals (pty) are an advanced and powerful technique in Python when we are dealing with terminal-based processes or simulating interactive sessions programmatically. A pseudo-terminal allows a process to connect to a real terminal. Python provides a built-in pty module that helps in creating and managing these terminal pairs. In this article, we will learn how to open a new pseudo-terminal pair using Python. Using pty.openpty() The pty module in Python provides the openpty() function that returns a tuple containing file descriptors for the master and slave ends of a new pseudo-terminal pair. Example Following is an example, which shows ... Read More

Specify Buffer Size When Opening a File in Python

Niharikaa Aitam
Updated on 15-May-2025 18:37:46

9K+ Views

When we open a file using the Python's built-in function open(), then the data temporarily stored in memory can be managed by setting the buffering parameter in the function. Buffering helps to improve the performance of opening a file by reducing the number of interactions with the disk during file input/output operations. Understanding the Buffering Parameter The buffering parameter in Python's open() function allows us to define how much data is stored in memory before being written to or read from the file. This buffering parameter is used to handle file operations with large data or frequent writes. Syntax Here ... Read More

U Modifier in Python File Opening

Niharikaa Aitam
Updated on 15-May-2025 18:35:11

1K+ Views

When the U modifier is used while opening a file then Python opens the file in Universal Newline mode. This mode enables Python to automatically detect and handle all common newline characters including , \r and \r during file reading. It is particularly useful when working with text files generated on various operating systems such as Windows, macOS or Linux which use different newline conventions. The U mode was used in Python 2 and early Python 3 versions to enable newline normalization. It allowed consistent handling of line endings regardless of the platform on which the file was created. However, ... Read More

Find Difference Between 2 Files in Python

Niharikaa Aitam
Updated on 15-May-2025 18:32:09

11K+ Views

In most of the applications, especially in data processing, software development or testing, it is required to compare two files to detect changes, validate outputs or find discrepancies. Python offers several ways to compare files which ranges from basic line-by-line comparisons to more advanced diff utilities. Following are the key methods which are used to compare two files in python - Line-by-line comparison: This is a straightforward approach to textual differences. difflib module: This module is used to produce human-readable diffs similar to Unix’s diff command. filecmp module: This module is used for quick binary or shallow comparisons. ... Read More

Scan Through a Directory Recursively in Python

Alekhya Nagulavancha
Updated on 15-May-2025 18:30:14

14K+ Views

A directory is simply defined as a collection of subdirectories and single files or either one of them. A directory hierarchy is constructed by organizing all the files and subdirectories within a main directory and this is also known as root directory. These subdirectories are separated using a / operator in a directory hierarchy. In python, we have different methods to scan through a directory recursively. In this article we are going to see about each method. Using os.walk() method Using glob.glob() method Using os.listdir() method ... Read More

Share Common Data Among Multiple Python Files

Niharikaa Aitam
Updated on 15-May-2025 18:28:13

2K+ Views

In Python, sharing common data such as constants, configuration settings or shared resources among multiple files is a routine task in modular application development. The data will be centralized in a dedicated module which allows it to be maintained in one place and imported wherever needed. Before proceeding with various approaches to share data among multiple Python files, first we need to understand what is a module and what is shared data. What is a Module? A Module is a Python file (.py) that contains definitions such as variables, functions, classes or runnable code. By placing shared data in its ... Read More

b Modifier in Python File Opening

Sarika Singh
Updated on 15-May-2025 18:23:46

13K+ Views

When we use the b modifier while opening a file, then 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, the 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

Check File Last Access Time Using Python

Sarika Singh
Updated on 15-May-2025 18:23:11

3K+ Views

Monitoring file access times is a common requirement for auditing, data management and cleanup of the 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 In Python, we can use the os.path.getatime() method to retrieve a path's most recent access time. The path that we need to verify for the access time is taken as the input argument by os.path.getatime() method. This method returns the amount of time since the epoch, as a floating point value. It throws one OSError if the requested path cannot be ... Read More

Advertisements