Files
Utilitary module to handle file operations
- class NamedFileLock(name)[source]
File-based lock mechanism for general locking purposes.
This class provides a way to track state across processes using a lock file. The lock file contains the PID of the capturing process.
- Usage:
- with NamedFileLock(name=”my_file”) as lock:
# Execute your operations
# Lock is automatically released
Initialize the named file lock mechanism.
- acquire()[source]
Acquire the lock.
- Return type:
- Raises:
RuntimeError – If a lock is already active
- property is_locked: bool
Check if a lock is currently active.
This checks if the lock file exists and if the PID in it is still running.
- Returns:
True if a lock is active, False otherwise
- Return type:
- property pid: int
Return the parent pid.
- create_folder(path, parents=False, mode=448)[source]
Try to create a new folder under the specified directory.
- Return type:
Notes
If the folder already exists, we will skip the directory creation and log an info message. That “check” is done by catching the
py:FileExistsErrorandpy:FileNotFoundErrorexceptions.
- guess_mimetype(attachment)[source]
Guess the mimetype of a given attachment.
- write_file(contents, path, mode=384)[source]
Try to write contents to a given file
- Return type:
Notes
If the folder already exists, we will skip the file creation and log an info message. That “check” is done by catching the
py:FileExistsErrorandpy:FileNotFoundErrorexceptions.