Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to put JavaScript code into a JS file through Python?

with open("C:scripts/CUAppVersion.js", 'a') as file:
file.write("if [CUID] = -1 then"
    "function GetCUID() { return 0; }"
"else"
    "function GetCUID() { return [CUID] });")

That is my code and it succesfully puts the JS into the JS file but it puts it on one line rather than as I typed it above. I am aware that my JS code isn't fully correct I do need to fix it.

How do I put that code into the JS file with the correct layout?

like image 346
Jill Avatar asked Mar 11 '23 20:03

Jill


1 Answers

Use multiline strings.

with open("C:/scripts/CUAppVersion.js", 'a') as file:
    file.write("""
if [CUID] = -1 then
    function GetCUID() { return 0; }
else
    function GetCUID() { return [CUID] });
""".strip())
like image 167
Rokas Kupstys Avatar answered Apr 03 '23 12:04

Rokas Kupstys