CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rclone-python

A python wrapper for rclone that makes rclone's functionality usable in python applications.

Pending
Overview
Eval results
Files

file-management.mddocs/

File Management

Essential file and directory management operations including creation, deletion, content access, and batch operations. Provides comprehensive control over remote file systems with support for all rclone command options.

Capabilities

Directory Operations

Create and manage directories on local and remote file systems.

def mkdir(path: str, args=None):
    """
    Creates directory if it doesn't already exist.
    
    Parameters:
    - path (str): Directory path to create ('remote:path' for remotes)
    - args (List[str]): Additional rclone mkdir flags
    
    Returns:
    None
    
    Raises:  
    RcloneException: If directory creation fails
    """

File Deletion

Delete files while preserving directory structure, or remove directories and their contents completely.

def delete(path: str, args=None):
    """
    Deletes files in a directory, preserving the directory structure itself.
    Removes all files in subdirectories but leaves empty directories.
    
    Parameters:
    - path (str): File or directory path to delete
    - args (List[str]): Additional rclone delete flags
    
    Returns:
    None
    
    Raises:
    RcloneException: If deletion fails
    """

def purge(path: str, args=None):
    """
    Completely removes directory and all its contents including subdirectories.
    Unlike delete, this removes the entire directory structure.
    
    Parameters:
    - path (str): Directory path to purge completely
    - args (List[str]): Additional rclone purge flags
    
    Returns:
    None
    
    Raises:
    RcloneException: If purge operation fails
    """

File Content Access

Read and output file contents with flexible filtering and positioning options.

def cat(path: str, count: Optional[int] = None, head: Optional[int] = None, 
        offset: Optional[int] = None, tail: Optional[int] = None, args=None) -> str:
    """
    Outputs contents of a single file with optional content filtering.
    
    Parameters:
    - path (str): File path to read ('remote:path' for remotes)
    - count (int, optional): Only output N characters total
    - head (int, optional): Only output first N characters  
    - offset (int, optional): Start output at character N (negative for end-relative)
    - tail (int, optional): Only output last N characters
    - args (List[str]): Additional rclone cat flags
    
    Returns:
    str: File contents as string
    
    Raises:
    RcloneException: If file read operation fails
    """

Usage Examples

Directory Management

from rclone_python import rclone

# Create directory on remote
rclone.mkdir('onedrive:Projects/NewProject')

# Create nested directories
rclone.mkdir('dropbox:Archive/2024/Documents')

# Create with specific permissions (where supported)
rclone.mkdir('sftp_server:uploads', args=['--dir-perms', '0755'])

File Deletion Operations

from rclone_python import rclone

# Delete specific file
rclone.delete('box:old_document.txt')

# Delete all files in directory (preserves directory structure)
rclone.delete('onedrive:TempFiles')

# Completely remove directory and contents
rclone.purge('gdrive:OldProject')

# Delete with confirmation bypass
rclone.delete('backup:expired_files', args=['--dry-run'])  # Preview first
rclone.delete('backup:expired_files')  # Execute deletion

File Content Operations

from rclone_python import rclone

# Read entire file
file_content = rclone.cat('onedrive:config.txt')
print(file_content)

# Read first 1000 characters
header = rclone.cat('dropbox:large_file.log', head=1000)

# Read last 500 characters  
tail_content = rclone.cat('box:error.log', tail=500)

# Read specific section
middle_section = rclone.cat('gdrive:data.csv', offset=1000, count=500)

# Read from specific position to end
from_position = rclone.cat('remote:document.txt', offset=250)

Batch Operations

from rclone_python import rclone

# Create multiple directories
directories = ['Projects/Web', 'Projects/Mobile', 'Projects/Desktop']
for dir_path in directories:
    rclone.mkdir(f'onedrive:{dir_path}')

# Clean up temporary files
temp_patterns = ['*.tmp', '*.cache', '~*']
for pattern in temp_patterns:
    rclone.delete(f'workspace:temp/{pattern}', args=['--include', pattern])

Safe Deletion with Verification

from rclone_python import rclone

def safe_delete(path, confirm_size_mb=None):
    """Safely delete with size verification"""
    
    # Check what will be deleted
    try:
        size_info = rclone.size(path)
        total_size_mb = size_info['bytes'] / (1024 * 1024)
        
        print(f"Will delete {size_info['count']} files ({total_size_mb:.2f} MB)")
        
        # Confirm for large deletions
        if confirm_size_mb and total_size_mb > confirm_size_mb:
            confirm = input(f"Delete {total_size_mb:.2f} MB? (yes/no): ")
            if confirm.lower() != 'yes':
                print("Deletion cancelled")
                return
        
        # Perform deletion
        rclone.delete(path)
        print("Deletion completed successfully")
        
    except Exception as e:
        print(f"Deletion failed: {e}")

# Use safe deletion
safe_delete('onedrive:old_backups', confirm_size_mb=100)

Content Processing

from rclone_python import rclone
import json

def read_config_file(remote_path):
    """Read and parse JSON config file"""
    try:
        content = rclone.cat(remote_path)
        return json.loads(content)
    except json.JSONDecodeError:
        print("Invalid JSON in config file")
        return None
    except Exception as e:
        print(f"Failed to read config: {e}")
        return None

# Read configuration from remote
config = read_config_file('onedrive:app/config.json')
if config:
    print(f"Loaded config with {len(config)} settings")

Log File Analysis

from rclone_python import rclone

def analyze_log_file(log_path, error_pattern="ERROR"):
    """Analyze log file for errors"""
    
    # Get file size first
    files = rclone.ls(log_path.rsplit(':', 1)[0], files_only=True)
    log_file = next((f for f in files if f['Name'] == log_path.split(':')[-1]), None)
    
    if not log_file:
        print("Log file not found")
        return
    
    file_size = log_file['Size']
    print(f"Analyzing log file: {file_size} bytes")
    
    # Read last 10KB for recent errors
    recent_content = rclone.cat(log_path, tail=10240)
    
    # Count error occurrences
    error_lines = [line for line in recent_content.split('\n') if error_pattern in line]
    
    print(f"Found {len(error_lines)} error lines in recent activity")
    if error_lines:
        print("Most recent errors:")
        for line in error_lines[-5:]:  # Show last 5 errors
            print(f"  {line.strip()}")

# Analyze remote log file
analyze_log_file('server:logs/application.log')

Directory Cleanup Workflows

from rclone_python import rclone
from datetime import datetime, timedelta

def cleanup_old_files(base_path, days_old=30):
    """Clean up files older than specified days"""
    
    # List all files
    all_files = rclone.ls(base_path, files_only=True)
    
    cutoff_date = datetime.now() - timedelta(days=days_old)
    old_files = []
    
    for file in all_files:
        # Parse modification time
        mod_time = datetime.fromisoformat(file['ModTime'].replace('Z', '+00:00'))
        if mod_time.replace(tzinfo=None) < cutoff_date:
            old_files.append(file)
    
    if old_files:
        total_size = sum(f['Size'] for f in old_files)
        print(f"Found {len(old_files)} old files ({total_size / (1024**2):.2f} MB)")
        
        # Delete old files individually for better control
        for file in old_files:
            file_path = f"{base_path}/{file['Name']}" if not base_path.endswith(':') else f"{base_path}{file['Name']}"
            try:
                rclone.delete(file_path)
                print(f"Deleted: {file['Name']}")
            except Exception as e:
                print(f"Failed to delete {file['Name']}: {e}")
    else:
        print("No old files found")

# Clean up files older than 30 days
cleanup_old_files('onedrive:TempStorage', days_old=30)

Advanced File Management Patterns

Atomic Operations

from rclone_python import rclone
import tempfile
import os

def atomic_file_update(remote_path, new_content):
    """Update remote file atomically via temporary file"""
    
    # Create temporary local file
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_file:
        temp_file.write(new_content)
        temp_path = temp_file.name
    
    try:
        # Upload to temporary remote location
        temp_remote = f"{remote_path}.tmp"
        rclone.copy(temp_path, temp_remote)
        
        # Move to final location (atomic on most backends)
        rclone.moveto(temp_remote, remote_path)
        
        print(f"Atomically updated {remote_path}")
        
    finally:
        # Clean up local temporary file
        os.unlink(temp_path)

# Update configuration file atomically
new_config = '{"version": "2.0", "enabled": true}'
atomic_file_update('onedrive:app/config.json', new_config)

Directory Synchronization Cleanup

from rclone_python import rclone

def clean_sync_destination(src_path, dest_path):
    """Remove files from destination that don't exist in source"""
    
    # Get source files
    src_files = {f['Name'] for f in rclone.ls(src_path, files_only=True)}
    
    # Get destination files  
    dest_files = rclone.ls(dest_path, files_only=True)
    
    # Find files to remove
    to_remove = [f for f in dest_files if f['Name'] not in src_files]
    
    if to_remove:
        print(f"Removing {len(to_remove)} orphaned files from destination")
        for file in to_remove:
            file_path = f"{dest_path}/{file['Name']}"
            rclone.delete(file_path)
            print(f"Removed: {file['Name']}")
    else:
        print("No orphaned files found in destination")

# Clean destination to match source
clean_sync_destination('local:source_dir', 'onedrive:backup_dir')

Install with Tessl CLI

npx tessl i tessl/pypi-rclone-python

docs

configuration.md

file-listing.md

file-management.md

file-transfer.md

hash-operations.md

index.md

public-links.md

remote-management.md

tile.json