pythonnpyscreen

Parse self as list, not string


Still trying to figure out this code im building and make it work cross-platform. I have a couple defining factors, that the code in question references, so I will input them all.

self.mainFile = r"\SYS64"
self.dir_path = os.path.dirname(os.path.realpath(__file__))
self.mainOSProgram = "python " + self.dir_path + self.mainFile + r"\jdosos.py"

Ive gotten it to work with

subprocess.Popen(["python",r'\Users\Terra Byte\Desktop\jdos3\JDOS3\SYS64\jdosos.py'])

but that defines a path, when I would like the path to be defined by the code, so it can work wherever the program is installed.

I get the same error, python: can't open file 'C:\Users\Terra': [Errno 2] No such file or directory

If I print(self.mainOSProgram), This is the result.

python C:\Users\Terra Byte\Desktop\jdos3\JDOS3\SYS64\jdosos.py

Which is the correct filepath, but its paring as a string, so it breaks the statement at the first space.


Solution

  • print(self.mainOSProgram) outputs string as is, so you get spaces. If you try to add quotes to your command it will work. python "C:\Users\Terra Byte\Desktop\jdos3\JDOS3\SYS64\jdosos.py"

    Regarding the code I would use os.path.join to construct the path

    import subprocess
    import os
    
    dir_path = os.path.dirname(os.path.realpath(__file__))
    
    executable_path = os.path.join(dir_path, "SYS64", "jdosos.py")
    subprocess.Popen(["python", executable_path])