
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 10476 Articles for Python

35K+ Views
In Python, it is a common scenario that the files we want to work with should be placed in the same folder as the script itself. Simply we with the reference of a filename like "data.txt" may not always work as expected, especially when the script is executed from a different working directory. The best way is to place the scripts in the actual location using the __file__ variable along with the os or pathlib modules. In this article, we are going to see the different approaches to open a file in the same directory as a Python script. Using ... Read More

3K+ Views
In Python, when working with files and directories, we may often need to access the parent directory of a given path, especially when going through a file system or managing relative paths in a project. In this article, we are going to explore the different methods available in Python to get the parent directory of the current or any specific path. Using os Module One of the commonly used methods to interact with the file system in Python is using the os module. This module provides various utilities to work with file paths and directories. To get the parent directory, ... Read More

13K+ Views
In Python, when working with files and directories, we may often need to get a list of subdirectories within a specific location, especially the current working directory. In this article, we are going to use the different methods available in Python to get a list of all subdirectories in the current directory. Using os Module While working with files, one of the most widely used approaches is using os module. This module provides a way to interact with the operating system to perform tasks such as navigating directories, reading file structures, and more. In this module, we have a ... Read More

19K+ 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

2K+ Views
In Python, the __init__.py is a special file in python packages. It is used to indicate that how a directory should be treated. When Python encounters a directory during an import, first it looks for the __init__.py file to determine how to initialize the package and what code should be executed during the import. In python 3.3 version, the __init__.py was introduced with the feature of creating a package with the namespace. Let's understand the purpose of the __init__.py file - It makes the directory a Python package so the interpreter can find the module ... Read More

8K+ 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 and also known as the "root" directory. Calculating the total size of a directory, i.e., all its files and subdirectories which is a common task in Python, especially when dealing with disk usage monitoring, backup management, or cleaning up storage. Python provides multiple ways to accomplish this efficiently using built-in modules such as os ... Read More

2K+ Views
In some applications, we have to find the files on our computer programmatically to make use of out time. Python provides several powerful libraries to search and locate files by making this task straightforward and efficient. In this article, we are going to see the different methods to find a file in our system using Python. Using the os Module The os module has a method os.walk(), which is used to search for a file in the directories and their subdirectories. This method returns the file paths in a directory which allows the user to search for a specific file. ... Read More

3K+ Views
The site-packages directory in python, is the place where third-party libraries and packages are installed. Knowning the site-packeges location is useful for debugging, verifying installations or inspecting package contents. This directory may vary depending on our operating system, Python version or based on the virtual environment we are using. In this article, we are going to explore the different ways to find the location of site packages directory in our working environments. Using site.getsitepackages() The getsitepackages() function of the site module, returns a list of global site-packages directories. This method may not work inside virtual environments and could be unavailable ... Read More

906 Views
When we are working with Python files or directories, we may often need to delete them to perform tasks such as cleaning up temporary files. Deleting directories isn't an easy task when compared with deleting files. In this article, we will explore all the different methods to delete a Python directory. Using shutil.rmtree() for Recursive Deletion The shutil module in Python has the rmtree()function, which is used to recursively delete a directory and all its contents. This function takes the directory path as the input parameter. Example Following is the example which uses the function shutil.rmtree() to delete a directory ... Read More

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