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:
- The method of getting shell command output in Python
- Windows git review reported an error. UnicodeDecodeError
- How to Fix Error in python3 | PIP install Dlib
- Explain stdin, stdout, stderr in C language
- [Linux] [kernel] bug: scheduling while atomic problem analysis
- Python module learning-Paramiko-Use python to throw an exception: Authentication failed.
- Attributeerror: ‘module’ object has no attribute ‘handlers’ — Python sub module import problem
- Under the “return-d” mode, the number of “return / return” is 1
- Python program exit: OS_ Exit() and sys.exit ()
- Error syntax error: invalid syntax in PIP install XXX
- The problem encountered in pyinstaller packaging “failed to execute script × *”
- Import sys module
- [SSL: CERTIFICATE_VERIFY_FAILED] Certificate Verify Failed (_ssl.C:579)
- Simple solution for node sass installation failure
- Package python3.1 + PyQt4 into exe
- Error gyp verb ‘which’ failed error: not found: python2 using Vue admin template master
- failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
- npm install Error: gyp verb `which` failed Error: not found: python
- [Python] error syntax error: summary of solutions to invalid syntax
- Error: pg_config executable not found.