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,124
  • 39
  • 95
  • 141

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
  • 82,471
  • 12
  • 195
  • 299
  • Lol :D It's **not** for me. It's part of a program that will create the said script onto a user's PC – TellMeWhy Jun 25 '15 at 19:06
  • 1
    ooooohhhhhh I understand :-D – TellMeWhy Jun 25 '15 at 19:18
  • I get this error `FileNotFoundError: [Errno 2] No such file or directory: '/home/devrobot/.config/autostart/resolutionx.desktop' ` – TellMeWhy Jun 25 '15 at 19:25
  • `python3 set_startupscript.py 'resolutionx' '/bin/bash resolutionx.sh'` – TellMeWhy Jun 25 '15 at 19:29
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/25203/discussion-between-jacob-vlijm-and-devrobot). – Jacob Vlijm Jun 25 '15 at 19:38
  • 1
    @DevRobot You probably never created a Startup Application before, so the directory `~/.config/autostart` didn't exist yet. Didn't count on that one in the script. fixed it here: http://askubuntu.com/a/598202/72216 please copy the latest version :) Nice catch! – Jacob Vlijm Jun 25 '15 at 19:51
  • I can't seem to log in for seem reason. btw the command returns nothing – TellMeWhy Jun 25 '15 at 19:51