Nahuel Robledo
Nahuel Robledo

Reputation: 11

Writting string to a File - 'str' object is not callable

i am generating a Json-like structure and I need write it to a File, any format of file, but i am getting this error. I have tried written it as Json and string without any luck.

import csv

score = []
test = ''
with open("score_test.txt") as f:
    spam = csv.reader(f, delimiter='|')
    for line in spam:
        score.append(line)

open = """{
    "info": {
        "_postman_id": "",
        "name": "GD-API-Testing-Update",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },

    "name": "Score_Tests",
    "item": [
        {
            "name": "Score_Ok",
            "item": ["""

for num, row in enumerate(score):
    test += """
                {
                    "name": "Score_Ok_%d",
                    "event": [
                        {
                            "listen": "test",
                            "script": {
                                "exec": [
                                    "pm.test(\"Status test of API-GD.\", function () {",
                                    "    pm.response.to.have.status(200);",
                                    "});",
                                    "",
                                    "pm.test(\"Score test.\", function () {",
                                    "    pm.response.to.not.be.error;",
                                    "    const responseJson = pm.response.json();",
                                    "    pm.expect(responseJson.gd_result).to.eql("%s");",
                                    "});"
                                ],
                                "type": "text/javascript"
                            }
                        }
                    ],
                    "request": {
                        "method": "GET",
                        "header": [
                            {
                                "key": "X-Access-Key-Id",
                                "value": "{{X-Access-Key-Id}}",
                                "type": "text"
                            },
                            {
                                "key": "X-Secret-Access-Key",
                                "value": "{{X-Secret-Access-Key}}",
                                "type": "text"
                            },
                            {
                                "key": "Accept-Version",
                                "value": "{{Accept-Version}}",
                                "type": "text"
                            }
                        ],
                        "url": {
                            "raw": "{{URL-API-GD}}/api/model/:model_id/mdn/:mdn",
                            "host": [
                                "{{URL-API-GD}}"
                            ],
                            "path": [
                                "api",
                                "model",
                                ":model_id",
                                "mdn",
                                ":mdn"
                            ],
                            "variable": [
                                {
                                    "key": "model_id",
                                    "value": "{{Test-Model}}"
                                },
                                {
                                    "key": "mdn",
                                    "value": "%s"
                                }
                            ]
                        }
                    },
                    "response": []
                }""" % (num, row[1], row[0])
    if num != len(score) - 1:
        test += ","

close = """           ]
        }
    ]
}"""

sample = open + test + close


with open("sample.txt", "a") as f:
    f.write(sample)

This is the error:

Traceback (most recent call last):
  File "/Users/user/Documents/training/TestForPostman/testGenerator.py", line 111, in <module>
    with open("sample.txt", "a") as f:
TypeError: 'str' object is not callable

The for Loop it is appending the generated string I need, and finally closed the structure at the end. I already checked the json structure and it is ok.

Upvotes: 1

Views: 379

Answers (1)

PB22
PB22

Reputation: 31

You are using open as variable in your code sample = open + test + close which is causing issue. Avoid using any reserved word as a variable. Rename the open variable with something else then it will work.

Upvotes: 1

Related Questions