How to use subprocess to return value?

I’m using MacBook 2019, python 3.10, and Panda3D 1.10.11.
I am not sure if my question is appropriate to post in the Panda 3D community. Please let me know if I’m doing it wrong.
I’m trying to use the system folder to select 3D files and import them into Panda3D.
When the program starts running, use Tkinter to open a folder and select a 3D model file. Then return the panda3D 3D model file path and create a 3D model.

Here is the

import subprocess
import sys

from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):
    def __init__(self):

        # quit when esc is pressed
        self.accept('escape', sys.exit)

        self.jack = self.loader.loadModel("jack")
        self.jack.setScale(2.0, 2.0, 2.0)
        self.jack.setPos(8, 50, 0)

        arch ="python3 /Users/gitdir/fyp/project/ | awk '{print $1}'",
                              universal_newlines=True, shell=True)

        model3D = arch


        model_path = model3D

        object = self.loader.loadModel(model_path)

        object.setPos(, (0, 30, 0))

app = MyApp()

I’m using awk to capture the file path.
The subprocess result:


<class 'str'>

Here is the

import os
import tkinter
from tkinter import filedialog

def getObj():
    window = tkinter.Tk()
    folder_selected = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select a File",
                                             filetypes=[('egg Data', '*.egg *.bam'), ('All Files', '*')])
    return folder_selected


The error message:

:loader(error): Couldn't load file /Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg
: does not exist.
Traceback (most recent call last):
  File "/Users/gitdir/fyp/project/", line 37, in <module>
    app = MyApp()
  File "/Users/gitdir/fyp/project/", line 31, in __init__
    object = self.loader.loadModel(model_path)
  File "/Users/opt/anaconda3/envs/py310/lib/python3.10/site-packages/direct/showbase/", line 298, in loadModel
    raise IOError(message)
OSError: Could not load model file(s): ['/Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg\n']

The Christmas tree model works when I change the code:

        object = self.loader.loadModel("/Users/tangkamsan/gitdir/fyp/panda3d/myModels/christmas_tree.egg")

I guess the mistake is about the ['/Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg\n'] which the path have \n. But I don’t know how to remove it.

By the way, if you have a better solution please let me know.

Thank you for reading my post. Have a nice day.

print() adds a newline to the end. You can pass an argument to it to prevent it from adding the newline, but it’s just as easy to call strip() on the string before passing it to loadModel.