Rajendra Dharmkar

Rajendra Dharmkar

160 Articles Published

Articles by Rajendra Dharmkar

Page 8 of 16

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 execute a Python file in Python shell?

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

When working with Python, you often need to execute Python files from within the Python shell or interactive environment. This allows you to test scripts, debug code, and interact with functions without leaving your current Python session. Using exec() Function The most common method is using the exec() function to read and execute a Python file − Example First, create a simple Python file called hello.py − # hello.py print("Hello from the script!") name = "Python" print(f"Welcome to {name} programming!") Now execute it from the Python shell − with open("hello.py", ...

Read More

How to set creation and modification date/time of a file using Python?

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

The creation and modification datetime of a file in Python are defined as the timestamps associated with when the file was created and when it was last modified. Creation datetime: It is defined as the timestamp when a file was initially created or added to the file system. Modification datetime: It is defined as the timestamp when the file's content was last modified or updated. These datetimes provide valuable information such as the file's age, recent changes, or when it was first introduced. In Python, you can retrieve these timestamps using functions like os.path.getctime() and os.path.getmtime(), and ...

Read More

How to create a unique directory name using Python?

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

Creating unique directory names is essential when working with temporary files or avoiding naming conflicts. Python's tempfile module provides a secure way to create unique temporary directories with proper permissions. Using tempfile.mkdtemp() The mkdtemp() function creates a temporary directory in the most secure manner possible. There are no race conditions in the directory's creation, and it's readable, writable, and searchable only by the creating user ID ? import tempfile import os # Create a unique temporary directory temp_dir_path = tempfile.mkdtemp() print(f"Created directory: {temp_dir_path}") # Check if directory exists print(f"Directory exists: {os.path.exists(temp_dir_path)}") # Clean ...

Read More

How to rename multiple files recursively using Python?

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

The act of renaming multiple files recursively in Python can be a useful task when it is required to change the names of multiple files within a directory and its subdirectories. If you need to replace certain characters, add prefixes or suffixes, or completely change the file names, Python has powerful tools to accomplish such operations. In this article, we explore different approaches to renaming multiple files recursively using Python ? Using os.walk() to Traverse the Directory Tree The os.walk() function from the os module is used to traverse the directory tree and access files and directories within ...

Read More

How to create a filesystem node using Python?

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

A filesystem node represents any entity in the file system, such as files, directories, or symbolic links. Python provides powerful modules like os and pathlib to create and manipulate these filesystem nodes programmatically. Filesystem nodes have attributes like names, sizes, permissions, and timestamps. You can create, rename, delete, and navigate through them to perform operations like reading files, creating directories, and checking properties. Creating Directories Using os.mkdir() The os.mkdir() function creates a single directory ? import os # Create a single directory directory_path = "new_directory" os.mkdir(directory_path) print(f"Directory '{directory_path}' created successfully!") ...

Read More

How to create and use a named pipe in Python?

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

Named pipes, also known as FIFOs (First-In, First-Out), are special files that enable inter-process communication in Python. Unlike anonymous pipes limited to parent-child processes, named pipes allow unrelated processes to exchange data seamlessly. Understanding Named Pipes Named pipes are special files that exist in the file system but don't store data permanently. They act as conduits where data flows from one process to another through system memory. A key characteristic is that they must be opened as either read-only or write-only, not both simultaneously. Creating a Named Pipe Use the os.mkfifo() function to create a named ...

Read More

How to Compose a Raw Device Number from the Major and Minor Device Numbers?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 403 Views

In low-level systems programming, device numbers play a crucial role in identifying and interacting with hardware devices. Every device connected to a computer system is assigned a unique pair of numbers: major and minor device numbers. Understanding how to compose a raw device number from these components is essential when working with device drivers or performing low-level device operations. Understanding Major and Minor Device Numbers In Linux kernel and Unix-like systems, major device numbers identify the device type or driver associated with a device, while minor device numbers specify a particular instance or unit of that device type. ...

Read More

How to get a file system information using Python?

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

File system information includes attributes and metadata associated with files or directories, such as size, available space, and usage statistics. Python provides several modules like os, shutil, and third-party libraries like psutil to retrieve this information programmatically. Using os.statvfs() for File System Statistics The os.statvfs() function returns detailed file system information as a statvfs_result object with various attributes ? import os # Use current directory for demonstration path = '.' # Retrieve file system information fs_info = os.statvfs(path) # Print key file system attributes print("File System Information:") print("Available File Nodes:", fs_info.f_favail) print("Total File ...

Read More

How can I get a file\'s permission mask using Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 362 Views

To get a file's permission mask in Python, use the os.stat() method from the os module. This method performs a stat system call on the given path and returns file information including permissions. Basic Usage of os.stat() The os.stat() function returns a 10-member tuple containing file metadata ? import os # Create a sample file first with open("sample.txt", "w") as f: f.write("Hello World") # Get file statistics st = os.stat("sample.txt") print("File stats tuple:", st) File stats tuple: os.stat_result(st_mode=33188, st_ino=123456, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=11, st_atime=1640995200, st_mtime=1640995200, st_ctime=1640995200) ...

Read More
Showing 71–80 of 160 articles
« Prev 1 6 7 8 9 10 16 Next »
Advertisements