Basic File Operations

This example demonstrates basic file operations using the File class.

Basic File Properties
"""
Basic file operations using the File class.

This demo shows fundamental file operations including:
- Creating and reading files
- Getting file metadata
- File copying and backup
- Text file operations
"""

import os

from true.collections import File, create_temp_file


def demo_file_creation():
    """Demonstrate basic file creation and properties."""
    print("\n=== File Creation and Properties ===")

    # Create a temporary text file

File Operations

File Operations Example
    file = File(temp_file)

    # Write some content
    file.write_text("Hello, World!\nThis is a test file.")

    # Display basic properties
    print(f"File path: {file.abspath}")
    print(f"Full path: {file.full_path}")
    print(f"Filename: {file.filename}")
    print(f"Extension: {file.extension}")
    print(f"Size: {file.size} bytes")
    print(f"MD5: {file.md5}")
    print(f"MIME type: {file.mime_type}")


def demo_file_operations():
    """Demonstrate file operations."""

File Statistics

File Statistics Example

    # Create a file with some content
    file = File("test_file.txt")
    file.write_text("Line 1\nLine 2\nLine 3")

    # Read content
    print("File content:")
    print(file.read_text())

    # Create backup
    backup = file.create_backup()
    print(f"\nBackup created: {backup}")

    # Copy file
    file.copy_to("test_file_copy.txt")
    print(f"File copied to: test_file_copy.txt")

    # Clean up
    try:
        os.remove(file.full_path)
        os.remove(backup.abspath)

Key Features

  • File creation and reading

  • File metadata access

  • File copying and backup

  • Text file operations

  • File statistics

  • MD5 hash calculation

  • MIME type detection