Python packaged *. Exe is running os.popen (cmd)/ subprocess.Popen *. Exe crashes with invalid [winerror 6] handle

Summary of the problem: last week I used Python3.8.5 to write an upgrade script for a software running on Windows 7, and by printing and exception catching found that the script crashed when the script was executed to os.popen(CMD), the exception was [WinError 6] handle invalid

Part of the test code:

logging.info('__Start__')
logging.info('os.system1')
logging.info('os.popen1')
try:
    logging.info(os.popen('c:'))
    logging.info('os.popen2')
except Exception as a:
    logging.info(a)
    logging.info('os.popen3')
logging.info('os.popen4')

Effect :

After testing, it was found that the program and script could run normally on Win10, and the script was executed separately on Win7 without any problem, and the script was called with the program on Win7, even if the script was executed OS.popen (‘ c: ‘) would crash.

Because os.popen is actually the encapsulation of subprocess.Popen, so the title also carries subprocess.Popen
solution: to implement a function myPopen, or use subproce. popen0 will be 1 stdin = Subprocess. DEVNULL to modify, the code is as follows:

def myPopen(cmd):
    proc = subprocess.Popen(cmd,
                            shell=True,
                            stdout=subprocess.PIPE,
                            stdin=subprocess.DEVNULL)
    return proc.stdout.read().decode()

Python running as Windows Service: OSError: [WinError 6] The handle is invalid
Popen(


Read More: