my python file is not being written

Asked

Viewed 55 times

0

I have this part of code:

with open('HTMLComposition.js', 'a', encoding="utf8") as JSIncludeFile:

    JSIncludeFile.write(Code = ("""
    //JavaFile
    DivsForComposition = """, divsForHTMLComposition, """
    for (let Div = 0 ; Div.lenght != 0 ; Div++ ){
        GetBodyTag = document.getElementById('ReportPlace')
        GetBodyTag.innerHTML += ${DivsForComposition}
        GetBodyTag.innerHTML += `<br>`
    }"""))

Don’t get too caught up in the details. the created file is not being written. It is simply created clean. What error in my code?

  • 2

    Must be some detail...

  • Just to stay here, lenght is misspelled and should be length.

1 answer

2

You call the method write of the open file passing strange arguments.

The method write of file type objects accepts a single argument, no name, with the text that must be written in the file.

Instead, you probably think that , concatenates strings in Python, tries to make the call with 3 arguments, the first with the name code (that does not exist in the function signature - only this would cause an exception of type TypeError). Then you close the string and place a comma, indicating a next argument - and this causes a syntax error: if your code is really the same as it is in the section you pasted, it should not even be executed

Ah - a second look, I understood why it is not happening a "Syntax" - you put an extra pair of parentheses after putting code = on the call to .write. What this does in Python is that the elements within this pair of parícses become a tuple: a sequence in which each comma-separated element is a distinct object. There’s no point in passing tuples to the .write - if it weren’t for the mistake of passing code= out of nowhere there, you’d have a Typeerror with that tuple.

You must have been mistaken for the print: a function that accepts any number of positional parameters, and prints the conversion of each of them to string in the standard output. But the , in print indicates the separation of parameters, not the concatenation of strings. Strings can be concatenated in Python with the operator + - as in Javascript. But this form is not recommended because it involves the defamation of many different symbols that end up disturbing the writing and reading of the code: "palavra 1 " + variavel_1 + "palavra 2" You have to close a string, concatenate another string, and concatenate the continuation of the string, which has to be opened again. In Python from version 3.6 onwards, we use "f-strings" - strings started with the prefix f before quotation marks, which allow Python expressions, including variables, to be pasted between keys ({}) inside the strings themselves. The previous example would be: f"palavra 1 { variavel_1 } palavra 2". (and in this case, if your string is to include other sets of keys, as is your case, you should use double keys, so that they are not interpreted as a Python expression);

with open('HTMLComposition.js', 'a', encoding="utf8") as JSIncludeFile:

    JSIncludeFile.write(f"""
    //JavaScriptFile
    DivsForComposition = { divsForHTMLComposition }
    for (let Div = 0 ; Div.lenght != 0 ; Div++ ){
        GetBodyTag = document.getElementById('ReportPlace')
        GetBodyTag.innerHTML += ${{DivsForComposition}}
        GetBodyTag.innerHTML += `<br>`
    }""")

And, some considerations about your code that don’t have directly to do with the current problem: you’re mixing Python, indented strings, Javascript (being called "Java" in the internal comment, I don’t know why), HTML, and a poor use of uppercase and lowercase variables in both Python and Javascript variable names in this short snippet. It’s certainly not the best practice for a large system - if it’s a small script, that’s fine - but if the Javascript chunk you have to generate is longer, it’s best to use a template system, and leave the code in Javascript, with the indications of variables and snippets to be replaced on the server side, in a separate file - see, for example, the documentation of Jinja2 which is a good templates engine for Python.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.