The File Watcher provides functionality to monitor changes in specific files or directories. This allows you to receive automatic notifications when files or directories are modified.
Basic Usage
Start Watching a File
To monitor changes in a specific file, use the following:
result = app.watch_file("path/file.txt")
if result:
print("File watching started")
else:
print("Failed to start file watching")
from pyloid import Pyloid
app = Pyloid(app_name="Pyloid-App", single_instance=True)
window = app.create_window(
title="Pyloid Browser",
)
window.load_url("https://www.example.com")
window.show_and_focus()
result = app.watch_file("/path/file.txt")
if result:
print("File watching started")
else:
print("Failed to start file watching")
app.run()
Start Watching a Directory
To monitor changes in a specific directory, use the following:
result = app.watch_directory("/path/directory")
if result:
print("Directory watching started")
else:
print("Failed to start directory watching")
from pyloid import Pyloid
app = Pyloid(app_name="Pyloid-App", single_instance=True)
window = app.create_window(
title="Pyloid Browser",
)
window.load_url("https://www.example.com")
window.show_and_focus()
result = app.watch_directory("/path/directory")
if result:
print("Directory watching started")
else:
print("Failed to start directory watching")
app.run()
Set File Change Callback
You can set a callback function to be executed when a file is changed:
def on_file_changed(path):
print(f"File has been changed: {path}")
app.set_file_change_callback(on_file_changed)
from pyloid import Pyloid
app = Pyloid(app_name="Pyloid-App", single_instance=True)
window = app.create_window(
title="Pyloid Browser",
)
window.load_url("https://www.example.com")
window.show_and_focus()
app.watch_file("/path/file.txt")
# Define callback function
def on_file_changed(path):
print(f"File has been changed: {path}")
# Set callback function
app.set_file_change_callback(on_file_changed)
app.run()
Set Directory Change Callback
You can set a callback function to be executed when a directory is changed:
def on_directory_changed(path):
print(f"Directory has been changed: {path}")
app.set_directory_change_callback(on_directory_changed)
from pyloid import Pyloid
app = Pyloid(app_name="Pyloid-App", single_instance=True)
window = app.create_window(
title="Pyloid Browser",
)
window.load_url("https://www.example.com")
window.show_and_focus()
app.watch_directory("/path/directory")
# Define callback function
def on_directory_changed(path):
print(f"Directory has been changed: {path}")
# Set callback function
app.set_directory_change_callback(on_directory_changed)
app.run()
Stop Watching
To stop watching a specific file or directory, use the following:
result = app.stop_watching("/path/to/file_or_directory")
if result:
print("Watching stopped successfully")
else:
print("Failed to stop watching")
from pyloid import Pyloid
app = Pyloid(app_name="Pyloid-App", single_instance=True)
window = app.create_window(
title="Pyloid Browser",
)
window.load_url("https://www.example.com")
window.show_and_focus()
result = app.stop_watching("/path/to/file_or_directory")
if result:
print("Watching stopped successfully")
else:
print("Failed to stop watching")
app.run()
Check Watched Paths
To check all currently watched paths (files and directories), use the following:
The File Watcher uses system resources, so use it only when necessary and clean up properly when finished.
Watching a large number of files or directories can impact system performance, so use caution.
Some file systems or operating systems may not detect certain types of changes.
This guide should help you understand the basic usage and advanced features of the File Watcher. If you have any additional questions or need further assistance, please don't hesitate to ask.