Developer Guide
Python Task API
Working with Files in Python Scripts
2 min
attachments as input python integrations can accept attachment fields as input attachment fields are stored as a list of dictionary objects since an attachment field may have many attachments each element of the list will be a dictionary that contains the keys 'base64' and 'filename' the 'base64' key contains the base64 encoding of the file contents the 'filename' key contains the file name in plain text example attachment object passed from swimlane to a python task attachments = \[{"base64" "agv5ighvdydzigl0igdvaw5npw==", "filename" "file1 txt"},{"base64" "ahr0cdovl3d3dy5zd2ltbgfuzs5jb20=", "filename" "file2 jpg"}] for example, to get the file name of the first file in an attachment field sw context inputs\['attachments']\[0]\['filename'] to get the file contents of the first file in an attachment field import base64 base64 b64decode(sw context inputs\['attachments']\[0]\['base64']) attaching files from python python integrations can add attachments to attachment fields in python by returning the attachment as a base 64 encoded string using sw outputs the value from sw outputs is then mapped into an attachment field using the integrations output tab, just like any other integration field value for example sw outputs append({"attachment" base64 b64encode(filecontent)}) specifying a file name by default the attachment file name is the md5 hash of the file contents the file name can be specified by using an inner dictionary to specify both the file name and base 64 file contents for example sw outputs append({"attachment" { "filename" "myfile txt", "base64" base64 b64encode(filecontent)}})