mozfile

3.0.0 · active · verified Thu Apr 16

mozfile is a Python convenience library providing robust file utilities primarily for use in Mozilla's automated testing environments. It offers enhanced replacements for standard library functions like `shutil.move` and `shutil.rmtree`, designed to handle common issues like file locking on Windows. The current stable version is 3.0.0, released in October 2022, with a stable but infrequent release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core utilities of `mozfile`: `move` and `remove`. These functions offer enhanced reliability over standard `shutil` operations, particularly in test environments and on Windows where file locking can be problematic. The `remove` function notably does not raise an error if the path does not exist.

import os
from mozfile import move, remove

# Create some dummy files/directories for demonstration
if not os.path.exists('test_dir_src'):
    os.makedirs('test_dir_src')
with open('test_dir_src/file1.txt', 'w') as f:
    f.write('hello world')

# 1. Use mozfile.move to move a file or directory
try:
    print(f"Moving 'test_dir_src/file1.txt' to 'file1_moved.txt'...")
    move('test_dir_src/file1.txt', 'file1_moved.txt')
    print(f"'file1_moved.txt' exists: {os.path.exists('file1_moved.txt')}")
finally:
    # Clean up after move
    if os.path.exists('file1_moved.txt'):
        os.remove('file1_moved.txt')

# 2. Use mozfile.remove to recursively delete a directory
# This version is more resilient to Windows file lock issues
print(f"Removing 'test_dir_src'...")
remove('test_dir_src')
print(f"'test_dir_src' exists: {os.path.exists('test_dir_src')}")

# 3. Demonstrate remove for non-existent path (no error)
print(f"Attempting to remove a non-existent path 'non_existent_dir'...")
remove('non_existent_dir')
print("No error raised, as expected for mozfile.remove on non-existent paths.")

view raw JSON →