After you installed this package, the next step is to import the package into your code and start using the functions.
from botcity.plugins.files import BotFilesPlugin
As a demonstration of the library, let's build a simple example together that will do something cool .
Working with ZIP files
With this plugin, you can easily zip and unzip folders and files as needed.
Using specific files
# Instantiate the plugin files = BotFilesPlugin() # List of all files that will be zipped files_path = ['documents/example_1.txt', 'images/test.png'] # Creates a zip file called "my_zip" containing the given files files.zip_files(files_path, "my_zip.zip")
Using entire folders
We can also create a zip file containing all the contents of the given directory, including files and subfolders.
# Instantiate the plugin files = BotFilesPlugin() # Creates a zip file called "result" containing all the "documents" directory content files.zip_directory("files/documents", "result.zip")
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, for example.
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 the plugin files = BotFilesPlugin() # Accessing a web page through the WebBot self.browse("https://speed.hetzner.de") # 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 self.find(label="download_file", matching=0.97, waiting_time=10000): self.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") ...
Check our examples and experiment with the API. Let us know where it can be improved.
Have fun automating!