![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 27104 Articles for Server Side Programming
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
15K+ 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
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
27K+ Views
It is an accepted fact that file handling is a fundamental operation in programming. And Python provides a broad set of tools to handle file operations so as to interact with files. Oftentimes, when working with Python scripts, it may be required to open a file located in the same directory as the script itself. This is a requirement often encountered when reading configuration files, accessing data files, or performing various file operations. In this extensive article, as you will find, we will explore various methods to open a file in the same directory as a Python script. We will ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
1K+ Views
In Python 3.4+ you can use the pathlib module to get the parent directory.Examplefrom pathlib import Path print(Path('/home/username').parent)OutputThis will give the output:/homeIn older versions, you can call the os.path.join on your path and '..'(represents parent directory) and then find its absolute path using os.path.abspath.Exampleimport os print(os.path.abspath(os.path.join('/home/username', '..')))OutputThis will give the output:/home
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
9K+ Views
To get a list of all subdirectories in a directory, recursively, you can use the os.walk function. It returns a three tuple with first entry being all the subdirectories.You can use it as follows:import os subdirs = [x[0] for x in os.walk('.')] print(subdirs)You can also list the directories(immediate only) using the os.listdir function and filtering the list using the os.path.isdir function.For exampleimport os d = '.' subdirs = [os.path.join(d, o) for o in os.listdir(d) if os.path.isdir(os.path.join(d,o))]
![Alekhya Nagulavancha](https://www.tutorialspoint.com/assets/profiles/535884/profile/60_4128029-1665726265.jpg)
16K+ Views
The path of a current file is defined with the help of directory hierarchy; and it contains the backtracked path from the current file to the root directory this file is present in. For instance, consider a file “my_file” belongs to a directory “my_directory”, the path for this file is defined as given below ./my_directory/my_file The directory, sub-directory and the file are all separated using the “/” separator in the path. Therefore, to get current file's full path, you can use the os.path.abspath() function. If you want only the directory path, you can call os.path.dirname() method. Using os.path.abspath() Method ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
2K+ Views
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable. An example of the __init__.py file:from math import sin from my_package import my_funcThe above code ensures that the imported functions are available in the package's modules when it is imported as it is defined ... Read More
![Alekhya Nagulavancha](https://www.tutorialspoint.com/assets/profiles/535884/profile/60_4128029-1665726265.jpg)
5K+ Views
A directory is simply defined as a collection of subdirectories and single files; or either one of them. These subdirectories are separated using a “/” operator in a directory hierarchy. A directory hierarchy is constructed by organizing all the files and subdirectories within a main directory, also known as “root” directory. When the size of a directory is to be calculated, we will consider it as a root directory and calculate the individual sizes of all the files and subdirectories (if any) present in it. Hence, to get the size of a directory we must walk through the hierarchy to ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
1K+ Views
Python stands out as one of the most adaptable and popular languages in the huge world of programming. Its ease of use and dependability make it a preferred option for developers working in a variety of fields. A frequent chore for programmers is looking for files on their machines. Finding files in Python is a crucial ability to have, whether you're organizing data, analyzing logs, or developing a file management solution. In this article, we'll examine a number of Python file-finding strategies and approaches. We'll discuss both the conventional and more sophisticated methods, giving detailed explanations to make sure you ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
2K+ Views
When it comes to Python development, one essential aspect is knowing how to locate the Python "site-packages" directory. This directory holds a plethora of third-party libraries and packages that enhance Python's capabilities and streamline the development process. Whether you are an experienced developer or just starting, understanding the precise location of the "site-packages" directory empowers you to effectively manage installed packages and enables manual interaction with them. In this comprehensive article, we will explore a variety of methods to pinpoint the location of the "site-packages" directory on different operating systems. From utilizing the versatile "sys" library to leveraging the comprehensive ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
409 Views
As a Python programmer, you might have often encountered situations where you need to delete directories, be it for cleaning up temporary files, organizing data, or managing project resources. However, deleting a directory in Python is not as straightforward or simple as removing a single file. It requires careful consideration of various factors to ensure a safe and efficient deletion process. In this ongoing article, we will explore different methods to delete a Python directory effectively. We will provide step−by−step explanations and code examples to help you through the process. We will cover essential topics such as handling errors, dealing ... Read More