Using subprocess or os.system to write a command to a shell within a shell

Solution for Using subprocess or os.system to write a command to a shell within a shell
is Given Below:

I’m trying to write a script that passes command line input to a connected Android device. I am able to successfully connect to the device with the subprocess command. However, at this point, I am no longer able to send any output to the adb shell. For the sake of clarity, I will say the first layer of the shell is the terminal window in VS code running Windows Powershell and the second shell would be the adb shell.

At this point, my terminal shows the adb shell prompt for the currently connected device. As soon as I exit, the rest of the code (code that I wanted to run inside the adb shell) proceeds to execute and error out due to being back on my local file system instead of the android device. Is there any way to use these commands to write to the CLI in my “current active shell” as opposed to only the first layer of the shell?

Ultimately, I would like to write commands to another shell that I enter on the Android device so I’m hoping that by solving this initial issue with commands not being sent to the adb shell, this will solve the potential upcoming issue with writing commands to yet another nested shell. Any suggestions would be greatly appreciated.