0

I'm creating a Python program for Ubuntu, and I need to write a python script into a file, but the \n in the code creates a new line in the file, it isn't copied as \n, so I get an EOL error.

This is the code for the Python script:

#!/usr/bin/env python3
import os
import sys
home = os.environ["HOME"]

name = sys.argv[1]; command = sys.argv[2]

launcher = ["[Desktop Entry]", "Name=resolutionx", "Exec=/bin/bash resolutionx.sh", "Type=Application", "X-GNOME-Autostart-enabled=true"]
file = home+"/.config/autostart/"+name.lower()+".desktop"

if not os.path.exists(file):
    with open(file, "wt") as out:     
        for l in launcher:
            l = l+name if l == "Name=resolutionx" else l
            l = l+command if l == "Exec=/bin/bash resolutionx.sh" else l
            out.write(l+"\n")
else:
  print("file exists, choose another name")

In this part:

if not os.path.exists(file):
    with open(file, "wt") as out:     
        for l in launcher:
            l = l+name if l == "Name=resolutionx" else l
            l = l+command if l == "Exec=/bin/bash resolutionx.sh" else l
            out.write(l+"\n")

The code for writing the above to the file is:

fStartUpScript = open("set_startupscript.py", "w")
fStartUpScript.write("""
#!/usr/bin/env python3
import os
import sys
home = os.environ["HOME"]

name = sys.argv[1]; command = sys.argv[2]

launcher = ["[Desktop Entry]", "Name=resolutionx", "Exec=/bin/bash resolutionx.sh", "Type=Application", "X-GNOME-Autostart-enabled=true"]
file = home+"/.config/autostart/"+name.lower()+".desktop"

if not os.path.exists(file):
    with open(file, "wt") as out:     
        for l in launcher:
            l = l+name if l == "Name=resolutionx" else l
            l = l+command if l == "Exec=/bin/bash resolutionx.sh"     else l
            out.write(l+"\\n")
else:
  print("file exists, choose another name")""")
fStartUpScript.close()

How can I solve this?

TellMeWhy
  • 17,484

1 Answers1

1

Understanding what the script should do

I am afraid you are misunderstanding how the script should be applied. There is no reason to have another script write this script into a file whatsoever.

That would be the same as writing an application to write an application; a huge detour, and a strange thing to do. you should simply create the script:

  1. Copy / paste the script from here into an empty file, save it as set_startupscript.py
  2. Call it with the right arguments:

    python3 /path/to/set_startupscript.py '<name>''<command>'
    

    where:

     '<name>'
    

    is the name of the launcher to be produced, between quotes, and

    '<command>'
    

    is the command to be run by the launcher, also between quotes.

Then it creates a launcher in ~/.config/autostart, which will run the command: '<command>' on log in.

The script itself is not to be used as a startup script, but to create a launcher in ~/.config/autostart to run a command.

Copy the script

IF you would need to copy the script for some reason (but again: why), or you need to copy any other file to another location, in python3, use:

shutil.copyfile(sourcefile, target)
Jacob Vlijm
  • 83,767