mozfile
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
-
[WinError 32] The process cannot access the file because it is being used by another process:
cause Attempting to move or remove files/directories on Windows that are still locked by another process or application (e.g., explorer, antivirus). Standard `shutil` functions often fail in this scenario.fixUse `mozfile.move()` or `mozfile.remove()` instead of `shutil.move()` or `shutil.rmtree()`. `mozfile` includes retry logic for such errors, significantly improving reliability in these situations. -
[Errno 13] Permission denied:
cause Similar to `WinError 32`, this often occurs when trying to modify or delete files/directories without sufficient permissions or when they are temporarily held by another process. This is common in automated testing environments.fixSwitch to `mozfile.move()` or `mozfile.remove()`. These functions are designed to mitigate these common permission/access denial issues by retrying the operation with a delay.
Warnings
- gotcha Unlike `shutil.rmtree`, `mozfile.remove` will *not* raise an error if the target path does not exist. This can mask issues if your code expects `FileNotFoundError`.
- gotcha When replacing `shutil.move` or `shutil.rmtree` with `mozfile.move` or `mozfile.remove`, be aware of the altered error handling. While `mozfile` functions are more robust against temporary file locking, they may behave differently in edge cases or with specific error types.
- breaking The jump from version 2.x to 3.0.0 (released October 2022) for `mozfile`, after a long gap, may include undocumented breaking changes, even if the primary API for core functions like `move` and `remove` appears stable. Always review release notes or test thoroughly when upgrading across major versions.
Install
-
pip install mozfile
Imports
- extract
from mozfile import extract
- move
import shutil; shutil.move(...)
from mozfile import move
- remove
import shutil; shutil.rmtree(...)
from mozfile import remove
Quickstart
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.")