Found 34471 Articles for Programming

How can I iterate over files in a given directory in Python?

Alekhya Nagulavancha
Updated on 19-Apr-2023 14:21:12

9K+ Views

Iterating over files in a given directory can be helpful for doing things like finding files that match a certain criterion, or counting the number of files in a directory. Python provides the following five ways to walk through all the existing files in a directory os.listdir() method os.walk() method os.scandir() method Using pathlib module glob.iglob() method Let us look at these methods in detail. Using os.listdir() Method The os.listdir() method is used to list all the files present in a directory. It accepts the path of the directory as an argument and all the entries, apart from ... Read More

What are vararg methods in Java?

Lakshmi Srinivas
Updated on 18-Feb-2020 10:02:14

165 Views

In Java methods, parameters accept arguments with three dots. These are known as variable arguments.sample(int args …){}If they are used you can pass a different number of arguments each time you call these methods.Examplepublic class Sample {    void demoMethod(String... args) {       for (String arg: args) {          System.out.println(arg);       }    }    public static void main(String args[] ) {       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap")    } }Outputram rahim robert krishna kasyap

What are variadic functions in Java?

Monica Mona
Updated on 16-Jun-2020 06:19:30

787 Views

Methods which uses variable arguments (varargs, arguments with three dots) are known as variadic functions.ExampleLive Demopublic class Sample {     void demoMethod(String... args) {       for (String arg: args) {          System.out.println(arg);       }     }    public static void main(String args[] ){       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap");    } }Outputram rahim robert krishna kasyap

How to list directory tree structure in python?

Rajendra Dharmkar
Updated on 11-Sep-2023 17:14:58

5K+ Views

While working with file systems and directories in Python, understanding the directories’ structure and their contents is essential for efficient file management and organization. Python makes available various methods and libraries that allow you to list the directory tree structure, including all subdirectories and files, in a comprehensive and exhaustive manner. Whether you're organizing files, analyzing directory structures, or performing data processing tasks, knowing how to list the directory tree structure in Python will significantly enhance your file handling capabilities. In this extensive article, we will explore different methods to list the directory tree structure in Python. We will also ... Read More

What are annotations in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

528 Views

Annotations are a tag (metadata) which provides info about a program. Annotations in Java Start with the symbol ‘@’. They are used by the compiler to detect errors. Software tools to generate code. They are used to show attributes of an element: e.g. @Deprecated, @Override. Annotation are used to describe the purpose of an element of the framework, e.g. @Entity, @TestCase, @WebService Annotations describe the behaviour of an element: @Statefull, @Transaction. Example Live Demo class Sample{ public void display(){ System.out.println(" "); } } public ... Read More

How to check if a given directory contains any other directory in Python?

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

3K+ Views

When dealing with and handling file systems and directory structures in Python, it's important to find if a given directory contains any other directories. This data becomes invaluable in various situations, such as organizing files, managing hierarchical data, or implementing conditional logic based on the presence of subdirectories. Python has provision for several methods and techniques to efficiently check for the existence of directories within a given directory. In this informative article, we will explore different methods to check if a given directory contains any other directories in Python. We will also provide step−by−step explanations and code examples to help ... Read More

How do you get a directory listing sorted by their name in Python?

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

9K+ Views

When working with directories in Python, it's often necessary to obtain a list of files and subdirectories within a given directory. Moreover, sorting this directory listing by their names can be beneficial for better organization and readability. Python provides several methods and techniques to achieve this goal efficiently. Sorting the directory listing alphabetically can help you quickly locate specific files or directories and facilitate various file management tasks. In this comprehensive article, we will explore different methods to get a directory listing sorted by name in Python. We will provide step−by−step explanations and code examples to guide you through the ... Read More

What is the difference between compositions and aggregations in Java?

Sharon Christine
Updated on 30-Jul-2019 22:30:20

154 Views

In Aggregation relationship among classes by which a class (object) can be made up of any combination of objects of other classes. It allows objects to be placed directly within the body of other classes.A composition is also a type of aggregation where the relationship is restrictive i.e. If two objects are in composition, the composed object will not exist without the other.

How to execute a static block without main method in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:20

1K+ Views

VM first looks for the main method (at least the latest versions) and then, starts executing the program including static block. Therefore, you cannot execute a static block without main method. Example public class Sample { static { System.out.println("Hello how are you"); } } Since the above program doesn’t have a main method, If you compile and execute it you will get an error message. C:\Sample>javac StaticBlockExample.java C:\Sample>java StaticBlockExample Error: Main method not found in class StaticBlockExample, please define the main method as: public static ... Read More

How to print new line in Java?

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

2K+ Views

The java.io.PrintStream.println() method prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println(). Using this method you can print the data on the console. import java.io.*; public class PrintStreamDemo { public static void main(String[] args) { char[] c = {'a', 'b', 'c'}; // create print stream object PrintStream ps = new PrintStream(System.out); // print an array and change line ps.println(c); ps.print("New Line"); // flush the stream ps.flush(); } } Output abc New Line

Advertisements