Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Python File Operations

Tech May 17 2

Path Types

Relative paths reference files in relation to the current working directory. A file in the same directory is referenced by its name. To access a parent directory, use ../. Absolute paths specify the full route from the root directory to the file. Relative paths are preferred for portability, as they allow projects to be moved without breaking references.

File Access Fundamentals

The open() function is used to access a file, returning a file object for subsequent operations. The mode parameter determines the allowed actions.

Available modes include: r (read), w (write), a (append), r+ (read/write), w+ (write/read), a+ (append/read), and their binary counterparts (rb, wb, etc.). The default mode is r.

Reading Files (r, rb)

The r mode is for text files, requiring an encoding specification. The rb mode reads binary data, such as images or audio, and does not accept an encoding argument.

Reading Entire Content

with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
    data = file_handle.read()
print(data)

Reading the entire file at once can consume significant memory for large files.

Reading a Specific Number of Characters

with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
    part1 = file_handle.read(4)
    part2 = file_handle.read(5)
print(part1)
print(part2)

In binary mode, read(n) retrieves n bytes.

Reading Line by Line

with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
    line = file_handle.readline()
    print(line.strip()) # strip() removes trailing newline

Reading All Lines into a List

with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
    lines_list = file_handle.readlines()
print(lines_list)

This method also poses a risk of high memory usage.

Iterative Reading

with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
    for line in file_handle:
        print(line, end='')

Iterating over the file object is memory-efficient.

Writing Files (w, wb)

The w mode creates a new file or overwrites an existing one. wb writes bytes and requires encoding conversion.

with open('warrior_profile.txt', mode='w', encoding='utf-8') as profile:
    profile.write("A disciplined fighter who has mastered the art of the blade.\n")
    profile.write("His calm demeanor hides a fiery spirit ready for battle.\n")

with open('warrior_profile.txt', mode='rb') as profile_bin:
    content_bytes = profile_bin.read()

Appending to Files (a, ab)

The a mode adds content to the end of a file without altering existing data.

with open('warrior_profile.txt', mode='a', encoding='utf-8') as profile:
    profile.write("He hails from the northern mountains.\n")

Combined Read and Write Modes

Read-Write (r+)

Allows reading and writing. The pointer starts at the beginning. Its crucial to read before writing to position the pointer correctly.

with open('warrior_profile.txt', mode='r+', encoding='utf-8') as profile:
    print(profile.read())
    profile.write("Origin: Northern Mountains")

Write-Read (w+)

Truncates the file and writes new content. Reading immediately after writing may yield no data.

with open('temp_data.txt', mode='w+', encoding='utf-8') as temp_file:
    temp_file.write("Temporary information")
    temp_file.seek(0)
    print(temp_file.read())

Append-Read (a+)

Appends data. The read pointer is at the end, so reading requires seeking to the start.

with open('log.txt', mode='a+', encoding='utf-8') as log_file:
    log_file.seek(0)
    print(log_file.read())
    log_file.write("New event recorded.\n")

Advanced File Manipulation

  • seek(offset, whence): Moves the file pointer. offset is the number of bytes. whence defines the reference point (0 for start, 1 for current, 2 for end).
  • tell(): Returns the current pointer position.
  • truncate(size): Truncates the file. If size is omitted, it truncates from the current position.
with open('data.txt', 'r+', encoding='utf-8') as file:
    file.seek(10)
    file.write('INSERTED TEXT')
    file.truncate()

Modifying File Content

To modify a file, read its content, make changes in memory, and write to a new file. Then, replace the old file.

import os

with open('source.txt', 'r', encoding='utf-8') as source, \
     open('temp_modified.txt', 'w', encoding='utf-8') as target:
    
    for line in source:
        modified_line = line.replace('old_value', 'new_value')
        target.write(modified_line)

os.remove('source.txt')
os.rename('temp_modified.txt', 'source.txt')

Processing line by line prevents excessive memory consumption.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.