How to fix file not opening when retrieving its path from a file

Question:

My program stores its configuration in a text file and retrieves it when you restart the program. When I first enter the configuration, it works, but when I restart the program it suddenly stops working. Any help is appreciated
Full code:

import os

datainfile = []

if os.path.isfile("config.txt"):
    cfg = open("config.txt", "r")
    for i in cfg:
        datainfile.append(i)
    cfg.close()
    a = datainfile[0]
else:
    cfg = open("config.txt", "w")
    a = "C:Program FilesGoogleChromeApplicationchrome.exe"
    cfg.write(a+"n")
    cfg.close()

os.system('"%s"' % a)

Before restarting:
enter image description here

After restarting and pressing the button:
enter image description here

Asked By: Tetrapak

||

Answers:

You likely need to remove the newline at the end of the filename:

def Run1():
    os.system('"%s"' % filename1.strip())

Also, please note that in general, you should avoid the use of os.system and use the appropriate function from the subprocess module instead.

Answered By: Some User
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.