datawriter = csv.writer(csvfile, fieldnames=fieldnames) TypeError: argument 1 must have a "write" method

I am new to Python and I don’t understand what this actually means with respect to the code or how to fix it. I am getting output written to the screen but I need it written to a csv. I can modify the code to open a blank file but that does me no good and I don’t get what is missing. There should be an easier way to do this.

import boto3
client = boto3.client('workspaces')
folder = 'd-92672c445d'
#folder = input('directoryId> ')

workspaces = client.describe_workspaces()['Workspaces']
directoryIds = [workspaces['DirectoryId']for workspaces in workspaces]
directoryIds.sort()


for DirectoryIds in folder:
    print(workspaces)

    
   
##----------------CSV Writer Code----------------##
import csv
with open('workspacesdata.csv', 'w') as fp:
    print('created', workspaces)


#with open('workspacesdata.csv', 'w', newline='') as csvfile:
   # datawriter = csv.writer(csvfile, workspaces, quotechar='"')
    
    fieldnames=['WorkspaceId', 'DirectoryId', 'UserName', 'IpAddress','State', 'BundleId', 'SubnetId','ComputerName','VolumeEncryptionKey' , 'WorkspaceProperties','RunningMode']
    csvfile=workspaces
    datawriter = csv.writer(csvfile, fieldnames=fieldnames)
    datawriter.writeheader()
    datawriter.writerows([workspaces])
    workspaces.write(str)
with open('workspacesdata.csv', 'w') as fp:
    print('created', workspaces)


##----------------CSV Reader Code----------------##

import os
os.system('start excel.exe workspacesdata.csv')