Server Side Programming Articles

Page 653 of 2109

How to open a file in the same directory as a Python script?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 37K+ 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 referencing 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 locate 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 the os ...

Read More

How do I get the parent directory in Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 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 navigating 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 ...

Read More

How to get a list of all sub-directories in the current directory using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 13K+ Views

In Python, when working with files and directories, we often need to get a list of subdirectories within a specific location, especially the current working directory. Python provides several methods to accomplish this task efficiently. Using os.listdir() with os.path.isdir() The os module provides a straightforward approach to interact with the operating system. We can combine os.listdir() with os.path.isdir() to filter only directories ? Example import os # List all sub-directories in the current directory subdirectories = [item for item in os.listdir('.') if os.path.isdir(item)] print("Sub-directories in current directory:") print(subdirectories) Sub-directories in ...

Read More

How to print full path of current file's directory in Python?

Alekhya Nagulavancha
Alekhya Nagulavancha
Updated on 24-Mar-2026 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 ...

Read More

What is __init__.py in Python?

Akshitha Mote
Akshitha Mote
Updated on 24-Mar-2026 2K+ Views

In Python, __init__.py is a special file that makes a directory a Python package. When Python encounters a directory during import, it looks for the __init__.py file to determine how to initialize the package and what code should be executed. The __init__.py file serves two main purposes: It makes the directory a Python package so the interpreter can find modules inside It can contain package initialization code, such as importing submodules, defining variables, or executing setup code Package Structure with __init__.py Here's a typical package structure ? mypackage/ ...

Read More

How to calculate a directory size using Python?

Alekhya Nagulavancha
Alekhya Nagulavancha
Updated on 24-Mar-2026 8K+ Views

A directory is simply defined as a collection of subdirectories and files. 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 the "root" directory. Calculating the total size of a directory, including all its files and subdirectories, 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 and pathlib. In this article, we ...

Read More

How to find a file using Python?

Niharikaa Aitam
Niharikaa Aitam
Updated on 24-Mar-2026 2K+ Views

In some applications, we need to find files on our computer programmatically to save time. Python provides several powerful libraries to search and locate files, making this task straightforward and efficient. In this article, we'll explore different methods to find files in our system using Python. Using the os Module The os module provides os.walk(), which searches for files in directories and their subdirectories. This method returns file paths in a directory, allowing you to search for specific files. Example Here's how to use os.walk() to find a specific file ? import os ...

Read More

How do I find the location of my Python site-packages directory?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 3K+ Views

The site-packages directory in Python is where third-party libraries and packages are installed. Knowing the site-packages location is useful for debugging, verifying installations, or inspecting package contents. This directory may vary depending on your operating system, Python version, or virtual environment. In this article, we will explore different ways to find the location of the site-packages directory in your working environment. Using site.getsitepackages() The getsitepackages() function from the site module returns a list of global site-packages directories. This method may not work inside virtual environments and could be unavailable in some restricted environments ? import ...

Read More

How to delete a Python directory effectively?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 1K+ Views

When working with Python files or directories, we may often need to delete them to perform tasks such as cleaning up temporary files. Deleting directories requires careful handling since they may contain nested files and subdirectories. In this article, we will explore different methods to delete a Python directory safely and effectively. Using shutil.rmtree() for Recursive Deletion The shutil module in Python provides the rmtree() function, which recursively deletes a directory and all its contents. This is the most common and efficient method for directory deletion. Example The following example uses shutil.rmtree() to delete a ...

Read More

How to scan through a directory recursively in Python?

Alekhya Nagulavancha
Alekhya Nagulavancha
Updated on 24-Mar-2026 15K+ Views

A directory is simply defined as a collection of subdirectories and files. A directory hierarchy is constructed by organizing all the files and subdirectories within a main directory, also known as the 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 with recursion Note: The directories ...

Read More
Showing 6521–6530 of 21,090 articles
« Prev 1 651 652 653 654 655 2109 Next »
Advertisements