Rajendra Dharmkar

Rajendra Dharmkar

160 Articles Published

Articles by Rajendra Dharmkar

Page 9 of 16

How to get stat of a file using Python?

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

Getting file statistics is essential for file processing and manipulation in Python. The os module provides built-in functions to retrieve file metadata like size, permissions, timestamps, and more. Using os.stat() for Basic File Stats The os.stat() function returns a named tuple containing various file statistics − import os # Create a sample file for demonstration with open('sample.txt', 'w') as f: f.write("Hello, World!") # Get the file stats file_stats = os.stat('sample.txt') # Access individual stats file_size = file_stats.st_size last_modified = file_stats.st_mtime creation_time = file_stats.st_ctime # Print the file stats ...

Read More

How to close all the opened files using Python?

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

In Python, file-handling tasks like opening, reading, writing, and closing files are common operations. While opening files has its significance, it's equally important that files are closed properly to release system resources and maintain data integrity. This article explores different methods to close multiple opened files in Python. Using Context Manager (Recommended) Context managers are the most Pythonic way to handle file operations. The with statement automatically manages setup and teardown operations, ensuring files are closed even if exceptions occur. Example Here's how to open and automatically close multiple files using context managers − ...

Read More

How to check the permissions of a directory using Python?

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

When working with files and directories in Python, checking permissions is essential to determine what operations can be performed. The os module provides convenient functions to check directory permissions. This article explores different methods to accomplish this task using practical code examples. Before we begin, ensure you have a basic understanding of Python and have Python installed on your system. Using os.access() Function The os.access() function is the most straightforward way to check directory permissions. It returns True if the specified permission is granted, False otherwise. Basic Permission Check Here's how to check individual permissions ...

Read More

How to check the permissions of a file using Python?

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

File permissions in Python determine who can read, write, or execute a file. Python's os module provides several functions to check these permissions, including os.access() and os.stat(). Understanding file permissions is crucial for security and access control in Python applications. Why Check File Permissions? File permission checks are important for several reasons: Control access to sensitive files and prevent unauthorized modifications Protect data integrity and confidentiality Enforce user-level security with different access levels Detect unauthorized changes to file permissions ...

Read More

How to create file of particular size in Python?

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

Creating a file of a specific size in Python can be accomplished using two main approaches: creating a sparse file (which doesn't actually allocate disk space) or creating a full file (which allocates the entire specified size on disk). Creating a Sparse File A sparse file appears to have the desired size but doesn't actually consume disk space until data is written. Use seek() to move to the target position and write a single byte ? # Create a 1GB sparse file with open('sparse_file.txt', 'wb') as f: f.seek(1024 * 1024 * 1024) ...

Read More

How can I do "cd" in Python?

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

You can change the current working directory in Python using the os.chdir() function from the os module. This function is equivalent to the cd command in terminal/command prompt. Syntax os.chdir(path) Parameters: path − A string representing the absolute or relative path of the directory you want to switch to Basic Example Here's how to change to a subdirectory ? import os # Get current directory print("Current directory:", os.getcwd()) # Change to a subdirectory (create it first for demo) os.makedirs('my_folder', exist_ok=True) os.chdir('my_folder') # Verify the change print("New ...

Read More

How to get the current open file line in Python?

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

When working with files in Python, you may need to determine the current line number while reading or processing the file. This article explores different methods to get the current line number of an open file using various Python functions and modules. Method 1: Using count() to Count Newlines The simplest approach is to read the entire file content and count the newline characters (""). Each newline represents the end of a line, so adding 1 gives us the total number of lines. First, let's create a sample file to work with ? # Create ...

Read More

How to set read and write position in a file in Python?

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

File positioning allows you to navigate to specific locations within a file for reading or writing operations. Python provides the seek() and tell() methods to control the file pointer position, enabling precise file manipulation. The seek() Method The seek() method moves the file pointer to a specified position. Its syntax is file.seek(offset, whence), where: offset: Number of bytes to move whence: Reference point (0=start, 1=current, 2=end) Setting Read Position You can set the read position to start reading from any location in the file ? ...

Read More

How to read only the first line of a file with Python?

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

Reading only the first line of a file is useful when you need to quickly access header information or metadata without loading the entire file into memory. Python provides several methods to accomplish this task efficiently. Using the readline() Method The readline() method is the most straightforward approach to read the first line of a file ? # Create a sample file content with open('sample.txt', 'w') as f: f.write("This is the first lineThis is the second lineThis is the third line") # Read only the first line with open('sample.txt', 'r') as ...

Read More

How do you append to a file with Python?

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

Appending content to a file is a common task in Python programming. Whether you need to log data, store user inputs, or update configuration files, appending allows you to add new information without overwriting existing content. This article explores various methods to append data to files in Python with practical examples. Basic File Append Syntax To append to a file, use the open() function with mode 'a' (append mode) ? # Basic syntax file = open('filename.txt', 'a') file.write('content to append') file.close() Appending a Single Line Here's how to append a single line of ...

Read More
Showing 81–90 of 160 articles
« Prev 1 7 8 9 10 11 16 Next »
Advertisements