rclone-python

0.1.24 · active · verified Thu Apr 16

rclone-python is a Python wrapper for the powerful rclone command-line tool, enabling programmatic interaction with various cloud storage providers. It provides a Pythonic interface for operations like copying, moving, syncing, listing, and managing files and remotes. The library is actively maintained, currently at version 0.1.24, and receives regular updates, often with minor bug fixes and feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `rclone_python` library. It includes checking for the rclone binary, performing a simple file copy operation, and listing the contents of a directory. It also shows basic error handling for `RcloneException`.

from rclone_python import rclone
from rclone_python.remote_types import RemoteTypes
import os

# Ensure rclone binary is installed and in PATH
if not rclone.is_installed():
    print("rclone CLI tool is not installed or not found in PATH.")
    print("Please install rclone: https://rclone.org/install/")
    exit(1)

print(f"rclone is installed: {rclone.is_installed()}")
print(f"rclone version: {rclone.version()['rclone_version']}")

# Example: Create a dummy local remote for demonstration
# In a real scenario, you'd configure a cloud remote.
# This assumes a 'local' type remote, no sensitive data.
# For actual cloud remotes, you would provide client_id, client_secret, etc.
# You might also use rclone.with_config(config_string) for in-memory config.

# Ensure a 'test_data' directory exists locally for the example
os.makedirs('./test_data', exist_ok=True)
with open('./test_data/hello.txt', 'w') as f:
    f.write('Hello, rclone-python!')

# A simple rclone copy operation
# Source: a local path, Destination: a local path (acting as a remote)
try:
    # This 'local_test' remote is created dynamically for the example.
    # For persistent remotes, use 'rclone config' or rclone.create_remote() once.
    # For this example, we'll use a direct path as 'remote'.
    # The rclone_python library primarily interfaces with the configured rclone remotes.
    # To simulate copying to a 'remote' which is just another local folder:
    # Create a destination folder that acts as our 'remote target'
    dest_path = './rclone_dest'
    os.makedirs(dest_path, exist_ok=True)
    print(f"\nCopying './test_data/hello.txt' to '{dest_path}'...")
    rclone.copy('./test_data', dest_path)
    print("Copy complete.")

    # List contents of the destination
    print(f"\nContents of '{dest_path}':")
    for item in rclone.ls(dest_path):
        print(f"- {item['Path']} (size: {item['Size']} bytes)")

except rclone.RcloneException as e:
    print(f"Rclone command failed: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →