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. popen
0 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:
- Python program uses OS. System () method to call exe program, resulting in no response of main program process
- error MSB6006: cmd.exe “exited with code 3
- Vs compilation error msb6006:“ cmd.exe ” exited with code 1.
- Record the python package EXE file running error importerror, but do not prompt specific error solution
- Python – [encoding] in Python os.system Solution to Chinese garbled code when calling CMD command
- Go compiles the EXE executable to remove the CMD window at run time
- When vs2017 compiles CUDA project, “error msb6006:“ cmd.exe ”Exited with code 1 Solutions for
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- Package python3.1 + PyQt4 into exe
- When using exe4j to generate exe executable file from jar, no JVM could be found on your system exception appears
- Compiling QT project under vs encountered “error 89error msb6006:“ cmd.exe ”Exited with code 3
- Running trust error reporting linker ‘link. Exe’ not found on Windows
- When python installs pocketsphinx module (package), an error is reported: command ‘swig.exe’ failed: No such file or directory
- Making Python script into exe command under Windows
- Install Python C extension compiler environment under windows (solve “error: command” cl.exe ‘ failed: No such file or directory”)
- Install python’s C extension compilation environment under windows (solve “error: command’cl.exe’ failed: No such file or directory”)
- OS error reported by Python: [errno 22] invalid argument
- Use subprocess to execute the command line, and the pipeline is blocked
- Error occurred when Python installed the pocketsphinx module (package): Command‘ swig.exe “Failed: no such file or directory
- Xilinx Vitis arm-xilinx-eabi-gcc.exe: error: *.c: Invalid argument