When working with files in Python, it's often useful to sort them based on their creation time. For example, to find the most recently added file or to process files in the order they were created then we need sort them by the creation date. Python provides built-in methods such as os and os.path which makes it easy to retrieve file metadata and sort directory contents accordingly. In this article, we will explore all the different method to sort the directories based on the creation date in Python - Using os.listdir() with sorted() and os.path.getctime() ... Read More
When we are working with files in Python we may need to copy only a certain type of file. Python makes this task simple and flexible by using built-in modules such as os and shutil and pathlib. Copying specific files using shutil and os modules When we want to copy only certain files such as all .txt files from one folder to another in Python then we can use the shutil and os modules. The shutil module is used to perform high-level operations on files such as file copying, moving, archiving and removing directories whereas os module is used to ... Read More
In Python, a Dictionary is a unordered and muttable data structure which stores data as a key-value pair. In some cases, we may need to merge two dictionaries into a single dictionary by using a single expression, in such scenarios python provides different methods. Using | Merge Operator The | merge operator in Python is used to merge multiple dictionaries into one with a single expression and returns the combined dictionary as a new dictionary. If both dictionaries have the same key, then the value from the dictionary on the right side of the operator will be replaced with the ... Read More
In this article, we will show you how to convert bytes into a string in Python. Strings are sequences of characters, while bytes are 8-bit values. To convert a sequence of characters to 8-bit values, we can utilize Python's built-in methods, which we will explore in this article. Converting Bytes to Strings in Python The following methods can be efficiently used to convert bytes to Python string. Using decode() function Using str() function Using codecs.decode() function Using pandas library ... Read More
Yes! We can use a pass statement in a Python if clause. This is used when a statement is required syntactically, but you do not want any command or code to execute. It represents a piece of code that will be added later, but initially, a placeholder is required to ensure the program runs without errors. And the if statement in Python evaluates whether a condition is true or false. So, the pass statement can be used in an if as well as an else block. Pass Statement in If Clause In this section, we will see ... Read More
Python Dictionary In Python, a dictionary is a collection of unique key-value pairs. Unlike lists, which use numeric indexing, dictionaries use immutable keys like strings, numbers, or tuples. Lists cannot be keys since they are mutable. Dictionaries are created using {} and store, retrieve, or delete values using their keys. The list() function returns all keys, sorting them. The in keyword checks for key existence, and replacing a key updates its value. Converting JavaScript to a Python Dictionary Python and JavaScript represent dictionaries differently, so an intermediate format is needed to exchange data between them. The most widely used format ... Read More
Python Dictionaries A dictionary in Python is a collection of key-value pairs. Each key is unique and maps to a specific value. For example- {"Title": "The Hobbit", "Author":"J.R.R. Tolkien", "Year": 1937} Dictionaries don't use numeric indexes and don't maintain a fixed order. We can't insert items in a specified position, as dictionaries store data based on keys, other than sequences. Where values can be strings, numbers, or float, and keys can be strings, numbers, or tuples. Lowercasing Python Dictionary Keys and Values To convert dictionary keys and values to lowercase in Python, we can loop through the dictionary and create ... Read More
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 approach or bottom-up approach. It ... Read More
In this article, we will learn how to find a unique character in a string in Java. A unique character is a character that occurs only once in the string, while all others can occur multiple times. We can solve this problem using the following ways: Using Brute Force method. Using HashMap. Using brute force method To find a unique character in a given string, we will use a brute force approach. In the brute force approach, we will use two loops to compare each character to the rest of the string. Let's look at the steps: ... Read More
We can read the contents of a web page in several ways using Java. Here, we are going to discuss three of them. Those are as follows: Using the java.net.URL class. Using the HttpClient library. Using the org.jsoup library. Using the URL class The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource (file or, directory or a reference) in the world wide web. The openStream() method of this class opens a connection to the URL represented by the current object and returns an InputStream object using which you ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP