iopath

0.1.10 · active · verified Sat Apr 11

iopath is a Python library from Facebook Research that provides an abstraction layer for I/O operations, allowing a unified API to interact with local filesystems and various remote storage services like S3 and HDFS. It simplifies reading from and writing to different storage backends. The current version is 0.1.10, and releases are infrequent, often tied to major projects from Facebook Research.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `PathManager` for basic file operations (write, read, check existence, remove) on a local filesystem. `PathManager` can transparently handle different schemes (e.g., `s3://`, `hdfs://`) once appropriate handlers are registered or if the optional dependencies are installed for common schemes like S3.

import os
from iopath.common.file_io import PathManager

# Initialize PathManager
pathmgr = PathManager()

# Define a local test file path
test_file_name = "iopath_test_file.txt"

# Ensure clean slate if previous run failed
if pathmgr.exists(test_file_name):
    pathmgr.rm(test_file_name)

print(f"Checking if '{test_file_name}' exists: {pathmgr.exists(test_file_name)}")

# Write content to the file using PathManager
content_to_write = "Hello from iopath! This is an I/O abstraction library."
with pathmgr.open(test_file_name, "w") as f:
    f.write(content_to_write)

print(f"After writing, does '{test_file_name}' exist? {pathmgr.exists(test_file_name)}")

# Read content from the file using PathManager
read_content = ""
with pathmgr.open(test_file_name, "r") as f:
    read_content = f.read()

print(f"Content read from '{test_file_name}':\n'{read_content}'")

# Clean up the test file
pathmgr.rm(test_file_name)
print(f"After removal, does '{test_file_name}' exist? {pathmgr.exists(test_file_name)}")

view raw JSON →