HomeCore ConceptsMastering File Handling in Python for All Developers

Mastering File Handling in Python for All Developers

- Advertisement -spot_img

File handling in Python, is a fundamental skill enabling developers to create, read, update, and delete files stored on a system. Python provides built-in functions and modules for handling files effectively, making it a versatile choice for tasks involving data storage and manipulation.

File handling refers to the process of creating, reading, writing, and manipulating files on a storage system. It is a common functionality in programming for tasks like saving user data, processing logs, and working with configurations. Here’s a brief overview of file handling in different programming languages:

Python simplifies file handling, enabling users to effortlessly read, write, and perform a variety of operations on files. Unlike other programming languages where file handling can be complicated or verbose, Python offers a straightforward and concise approach, making it an ideal choice for beginners and professionals alike.

File Handling in Python

If you want to dive deeper into Python file handling, our Python Self-Paced Course is the perfect resource. It covers everything from basic to advanced Python concepts, helping you build a strong foundation.

Python categorizes files as either text files or binary files, and this distinction is crucial. A text file comprises a sequence of characters, with each line ending in a special character known as the End of Line (EOL) character, such as a comma (,) or newline character. These characters signify the end of one line and the start of a new one.


Types of Files – File Handling in Python,

Python supports handling different types of files, primarily:

  1. Text Files
  • Contain readable characters organized into lines.
  • Examples: .txt, .csv, .json
  1. Binary Files
  • Contain binary data that is not human-readable.
  • Examples: .jpg, .exe, .dat

Opening Files

To work with a file, it first needs to be opened. Python provides the open() function, which takes two main arguments:

  1. File Name: The name of the file to open.
  2. Mode: Specifies the purpose for opening the file.

Modes in open()

ModeDescription
rRead (default mode).
wWrite (creates or truncates).
xCreate (fails if file exists).
aAppend to the file.
bBinary mode.
tText mode (default).
+Read and write.

Example:

file = open("example.txt", "r")

Reading Files – File Handling in Python

Methods to Read:

  1. read(): Reads the entire file or a specified number of characters.
   with open("example.txt", "r") as file:
       content = file.read()
       print(content)
  1. readline(): Reads a single line from the file.
   with open("example.txt", "r") as file:
       line = file.readline()
       print(line)
  1. readlines(): Reads all lines into a list.
   with open("example.txt", "r") as file:
       lines = file.readlines()
       print(lines)

Writing Files

Methods to Write:

  1. write(): Writes a string to the file.
   with open("example.txt", "w") as file:
       file.write("Hello, World!")
  1. writelines(): Writes a list of strings.
   with open("example.txt", "w") as file:
       file.writelines(["Hello\n", "World\n"])

Appending Files – File Handling in Python

Using the a mode, you can add content without overwriting existing data.

with open("example.txt", "a") as file:
    file.write("Appending new content.\n")

Closing Files – Handling the Files in Python

Although Python’s with statement automatically closes files, you can use file.close() explicitly.

Example:

file = open("example.txt", "r")
content = file.read()
file.close()

File Iteration

Files can be iterated over line by line using a loop:

with open("example.txt", "r") as file:
    for line in file:
        print(line.strip())

Working with Binary Files

Binary files require the b mode for operations.

Example:

with open("example.jpg", "rb") as file:
    binary_data = file.read()

To write binary data:

with open("output.jpg", "wb") as file:
    file.write(binary_data)

Handling File Exceptions

Python provides robust exception handling for file operations using try-except blocks.

Example:

try:
    with open("example.txt", "r") as file:
        content = file.read()
except FileNotFoundError:
    print("File not found.")
except IOError:
    print("IO error occurred.")

File Methods and Attributes

  1. file.name: Returns the file name.
  2. file.mode: Returns the mode in which the file was opened.
  3. file.closed: Checks if the file is closed.

Example:

file = open("example.txt", "r")
print(file.name)  # example.txt
print(file.mode)  # r
file.close()
print(file.closed)  # True

File Paths

Python supports:

  1. Absolute Paths: Complete path starting from the root directory.
  2. Relative Paths: Path relative to the current working directory.

Example:

# Absolute Path
open("C:/Users/Username/Documents/example.txt", "r")

# Relative Path
open("./example.txt", "r")

Deleting Files

Python’s os module is used to delete files:

Example:

import os
os.remove("example.txt")

To check if the file exists before deletion:

if os.path.exists("example.txt"):
    os.remove("example.txt")
else:
    print("File does not exist.")

Working with Directories

Python’s os module provides functions for directory handling:

  1. os.mkdir(): Creates a new directory.
  2. os.rmdir(): Removes an empty directory.
  3. os.listdir(): Lists all files and directories.

Example:

import os

# Create a directory
os.mkdir("new_folder")

# List contents
print(os.listdir("."))

# Remove a directory
os.rmdir("new_folder")

Advanced File Handling

File Handling with shutil

Python’s shutil module supports advanced file operations, such as copying and moving files.

Example:

import shutil

# Copy file
shutil.copy("example.txt", "copy_example.txt")

# Move file
shutil.move("example.txt", "new_directory/example.txt")

Working with pathlib

The pathlib module offers object-oriented file and directory manipulation.

Example:

from pathlib import Path

# Create a Path object
file_path = Path("example.txt")

# Check if file exists
if file_path.exists():
    print("File exists.")

# Read file content
print(file_path.read_text())

File Compression

Python supports file compression using the zipfile and gzip modules.

Using zipfile

Example:

import zipfile

# Create a ZIP file
with zipfile.ZipFile("example.zip", "w") as zipf:
    zipf.write("example.txt")

# Extract a ZIP file
with zipfile.ZipFile("example.zip", "r") as zipf:
    zipf.extractall("output_folder")

Using gzip

Example:

import gzip

# Compress file
with open("example.txt", "rb") as f_in:
    with gzip.open("example.txt.gz", "wb") as f_out:
        f_out.writelines(f_in)

# Decompress file
with gzip.open("example.txt.gz", "rb") as f_in:
    with open("example_dec.txt", "wb") as f_out:
        f_out.writelines(f_in)

Summary

Python’s file handling capabilities are extensive, offering developers powerful tools for interacting with files and directories. With robust error handling, advanced modules like pathlib and shutil, and support for various file types and formats, Python makes it easy to manage and manipulate data stored on disk.

Read This also

Top 40 Python interview questions and answers

Stay Connected
16,985FansLike
2,458FollowersFollow
61,453SubscribersSubscribe
Must Read
Related News

LEAVE A REPLY

Please enter your comment!
Please enter your name here