minute read
Files
You are reading our Automation1 API documentation for the Python™ programming language.
The Basics
In the Python API, Files lets you read and write controller files on the Automation1 controller. Each Automation1 controller has its own file system in which you can store the controller files.
How to Use
Similar to other file systems, Files lets you do all the options that follow:
- Read and write controller files. You can read or write controller files as string text or as byte arrays.
- Get a list of all the controller files.
- Delete controller files.
When you read or write a controller file, you read or write all of its contents. You cannot open or change part of a controller file.
To access one or more of the controller files, use the Controller.files property. Refer to the example that follows.
contents = controller.files.read_bytes(controller_file_name)
controller.files.write_bytes(controller_file_name, file_bytes_content)
contents = controller.files.read_text(controller_file_name)
controller.files.write_text(controller_file_name, file_text_content)
controller.files.delete(controller_file_name)
all_files = controller.files.get_files()
Example Code
import automation1 as a1
controller = a1.Controller.connect()
controller.files.write_text("MyExampleFile.txt", "Hello world!")
file_contents = controller.files.read_text("MyExampleFile.txt")
print(file_contents)
controller.files.delete("MyExampleFile.txt")
Thread Safety
The threading library is the only multithreading library that is officially supported by the Automation1 Python API.
All the methods and properties that operate under the Controller.files property are thread safe. You can call them from two or more threads without interference.
Full Reference
For more information about all the methods that are available for Files, refer to the list that follows.
Controller.files Methods
Deletes a controller file on the Automation1 controller.
controller_file_name: The file to delete.
Returns the names of all the controller files (including their paths) on the Automation1 controller.
Returns: A list of names of all the controller files (including their paths), or an empty list if no files exist.
Returns the contents of a controller file from the Automation1 controller as raw bytes.
controllerFileName: The file to read from.
Returns: The file contents as bytes.
Returns the contents of a controller file from the Automation1 controller as UTF-8 encoded text.
controller_file_name: The file to read from.
encoding: The text encoding to use.
Returns: The text file contents encoded with the specified encoding.
Creates a new controller file on the Automation1 controller and writes the specified contents to it. This method will overwrite any existing file that has the same file name.
controller_file_name: The file to write to.
contents: The bytes of a file's contents to write.
Creates a new controller file on the Automation1 controller and writes the specified contents to it as text with the specified encoding. This method will overwrite any existing file that has the same file name.
controller_file_name: The file to write to.
contents: The text contents to write.
encoding: The text encoding to use.