File Handling¶
Monitor your file system for new files, compress and decompress files and folders using our new Files plugin.
Installation¶
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
Working with ZIP files¶
With this plugin, you can easily zip and unzip folders and files as needed.
Using specific files¶
Using entire folders¶
We can also create a zip file containing all the contents of the given directory, including files and subfolders.
Unzipping files¶
It is possible to unzip only a specific file or the entire contents of a zip file.
# Instantiate the plugin
files = BotFilesPlugin()
# Unzipping all contents of "my_zip" and saving to "zip_content" folder
files.unzip_all(zip_file="my_zip.zip", destination_folder="files/zip_content")
# Unzipping only a single file contained in "my_zip"
files.unzip_file(zip_file="my_zip.zip", file_to_extract="image.png")
Waiting for files¶
In some cases it is necessary to wait for files to be saved and available in certain folders, such as processes where files are downloaded from a website.
In these cases we may not know the exaclty name and path of the file that will be downloaded, making the process of waiting for the file more complicated. Also, when working with large files we may not know exactly how long it takes to wait for the file, affecting the rest of the process.
With that in mind, the wait_for_file
method offers a way to wait for a new file without any difficulties.
It is possible to monitor a destination folder through a context manager
until a new file is completely created or modified.
from botcity.plugins.files import BotFilesPlugin
from botcity.web import WebBot
...
# Instantiating o webbot
bot = WebBot()
# Setting the default browser
bot.browser = Browser.FIREFOX
# Setting the WebDriver
bot.driver_path = r"geckodriver.exe"
# Instantiating the plugin
files = BotFilesPlugin()
# Accessing a web page through the WebBot
bot.browse("https://filesamples.com/formats/bin")
# Waiting for a new ".bin" file to be saved in the "downloads" folder
with files.wait_for_file(
directory_path="Documents/downloads",
file_extension=".bin",
timeout=300000):
print("\nDownloading file...")
# Clicking to start download
if bot.find(label="download_file", matching=0.97, waiting_time=10000):
bot.click()
# Continuing the process after waiting for the file
print("\nDownload completed, continuing the process...")
# Getting the full path of the newest ".bin" file in the "downloads" folder
file_path = files.get_last_created_file(
directory_path="Documents/downloads",
file_extension=".bin")
...