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)
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.
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)
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.