Found 34470 Articles for Programming

How do you get a directory listing sorted by creation date in Python?

Rajendra Dharmkar
Updated on 11-Sep-2023 16:23:26

6K+ Views

In the process of managing files and directories in Python, there are often situations where you need to obtain a directory listing sorted by creation date. The task of sorting files and directories by their creation timestamps can be beneficial for various purposes, such as analyzing the most recently added files or organizing data based on chronological order. Python has several methods and techniques to achieve this goal effectively and efficiently. By making use of the "os" module, the "pathlib" module, or third−party libraries, you can effortlessly obtain a sorted directory listing based on creation date. In this exhaustive article, ... Read More

How to declare, create, initialize and access an array in Java?

Sharon Christine
Updated on 25-Feb-2020 10:56:19

245 Views

You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}

How to know/change current directory in Python shell?

Sarika Singh
Updated on 26-Aug-2023 08:05:42

38K+ Views

A portable method of interacting with the operating system is offered through the OS Python Module. The module, which is a part of the default Python library, contains tools for locating and modifying the working directory. The following contents are described in this article. How to obtain the current working directory: os.getcwd() Changing the current working directory: os.chdir() The __file__ function returns the path to the current script file (.py). Getting the current working directory- os.getcwd() The function os.getcwd() returns as a string str the absolute path to Python's current working directory. "Get current working directory" (getcwd) refers ... Read More

How to check if a file is a directory or a regular file in Python?

Rajendra Dharmkar
Updated on 11-Sep-2023 16:35:54

453 Views

When working with files and directories in Python, it's a crucial point to distinguish between regular files and directories. Knowing the type of each file is most essential for implementing different file handling operations, such as reading, writing, or navigating through directory structures. Python makes available several methods and techniques to determine if a given path points to a directory or a regular file. By utilizing the "os" module, the "pathlib" module, or other specialized functions, you can easily differentiate between these two types and perform the necessary operations accordingly. In this exhaustive article, we will explore different methods to ... Read More

What is the purpose of a default constructor in Java?

varma
Updated on 12-Mar-2020 05:21:59

2K+ Views

Default constructors in Java:A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. There are two types of constructors namely −parameterized constructors − Constructors with arguments.no-arg constructors − Constructors without arguments.Example Live Demopublic class Sample{    int num;    Sample(){       num = 100;    }    Sample(int num){       this.num = num;    }    public static void main(String args[]){       System.out.println(new Sample().num);       System.out.println(new Sample(1000).num);    } }Output100 1000Default ConstructorIt ... Read More

How to safely open/close files in Python?

Rajendra Dharmkar
Updated on 03-Aug-2023 12:50:29

2K+ Views

When it comes to file operations in Python, adhering to best practices is crucial to ensure the security and integrity of your data. Mishandling files can lead to data corruption, resource leaks, and even security vulnerabilities. This article aims to delve into the best practices for safely opening and closing files in Python, accompanied by five illustrative code examples with stepwise explanations to enhance your understanding. Utilizing the 'with' Statement Python offers a convenient approach to opening and closing files using the 'with' statement. The 'with' statement guarantees automatic closure of the file once the code block inside ... Read More

How to delete all files in a directory with Python?

Rajendra Dharmkar
Updated on 03-Aug-2023 10:59:31

16K+ Views

When it comes to file management within a directory in Python, there can be scenarios where you find yourself needing to entirely empty the folder, erasing all the files and sometimes subdirectories it contains. In this regard, Python makes provision for several efficient and secure methods to accomplish this task. In this article, we'll explore a few diverse methods to achieve the above-mentioned objective of deleting all files in a directory. We take the help of code examples accompanied by step-by-step explanations to ensure a smooth execution of the aforementioned task. Utilizing os.listdir() and os.remove() Let's start with ... Read More

Can a constructor be overridden in java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

13K+ Views

If super class and sub class have same methods including name, return type and parameters, and if you try to call it using the object of the sub class Then the method in the sub class is invoked. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden. If you try to write a super class’s constructor in the sub class compiler treats it as a method and expects a return type and generates a compile time error. Example ... Read More

What is the class "class" in Java?

seetha
Updated on 30-Jul-2019 22:30:20

410 Views

The Java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor. Example Following is the example demonstrates the usage of the class Class. The java.lang.Class.getCanonicalName() method returns the canonical name of the underlying class as defined by the Java Language Specification. It returns null if the class does not have a canonical name. Live Demo import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = ... Read More

Can constructors be marked final, abstract or static in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

2K+ Views

Except public, protected and, private constructor does not allow any other modifier. When you use a final keyword with a method or constructor it cannot be overridden. But, a constructor in Java cannot be overridden therefore, there is no need of using the final keyword with the constructor. Since you cannot override a constructor you cannot provide body to it if it is made abstract. Therefore, you cannot use abstract keyword with the constructor. If you want to invoke a member of a class before instantiating the class you need to use static before it. But, contractors are called ... Read More

Advertisements