Registry / testing / mozfile

mozfile

JSON →
library3.0.0pypypi✓ verified 85d ago

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.

pip install mozfile
INSTALL
IMPORT
SIG · MOZFILE
M
mozfile
testingpythonv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

extract
from mozfile import extract
move
from mozfile import move
import shutil; shutil.move(...)
mozfile.move provides enhanced robustness, especially on Windows, by retrying operations on common file access errors.
remove
from mozfile import remove
import shutil; shutil.rmtree(...)
mozfile.remove provides enhanced robustness, especially on Windows, by retrying operations and suppressing errors for non-existent paths (unlike shutil.rmtree).

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.")
Debug
Known issues
gotchaUnlike `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`.
fix
Explicitly check `os.path.exists()` before calling `mozfile.remove()` if you need to ensure the path existed, or adjust error handling to account for silent removal of non-existent paths.
affects: All versions
gotchaWhen 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.
fix
Thoroughly test existing code after migrating from `shutil` to `mozfile` to ensure compatibility with altered error handling and retry logic, especially for non-Windows environments where `shutil` might be sufficient.
affects: All versions
breakingThe 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.
fix
Consult `mozbase`'s official documentation and conduct comprehensive regression testing when upgrading to 3.0.0 from older major versions to identify any subtle behavior changes.
affects: 2.x to 3.0.0
Errors
Common errors & fixes
[WinError 32] The process cannot access the file because it is being used by another process:
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.
fix
Use `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:
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.
fix
Switch 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.
Upgrade
Version history
3.0.0latest on PyPI · released Oct 13, 2022
Audit
Dependencies
orjsonoptionalUsed as a compatibility shim for JSON operations if available, falling back to `stdlib json`. It is an optional dependency.
Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources
mozfile — pip install mozfile · libregistry