这个问题在Python 3.7及以上版本中默认已经解决
这绝对是一个棘手的技巧:答案是在使用subprocess
模块之前,遍历已经打开的文件描述符。
def _hack_windows_subprocess():
"""HACK: python 2.7 file descriptors.
This magic hack fixes https://bugs.python.org/issue19575
by adding HANDLE_FLAG_INHERIT to all already opened file descriptors.
"""
import stat
from ctypes import windll, wintypes
from msvcrt import get_osfhandle
HANDLE_FLAG_INHERIT = 0x00000001
for fd in range(100):
try:
s = os.fstat(fd)
except:
continue
if stat.S_ISREG(s.st_mode):
handle = wintypes.HANDLE(get_osfhandle(fd))
mask = wintypes.DWORD(HANDLE_FLAG_INHERIT)
flags = wintypes.DWORD(0)
windll.kernel32.SetHandleInformation(handle, mask, flags)
这是一段没有它就会崩溃的示例代码:
import os, subprocess
f = open("a.txt", "w")
subprocess.Popen(["cmd"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
f.close()
os.remove(f.name)
追溯(Traceback)(最近的调用在最上面):
文件 "stdin",第 1 行,模块中
WindowsError: [错误 32] 进程无法访问文件,因为该文件正被另一个进程使用:'a.txt'
现在已经修复:
import os, subprocess
f = open("a.txt", "w")
_hack_windows_subprocess()
subprocess.Popen(["cmd"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
f.close()
os.remove(f.name)
操作正常。
希望我有所帮助。
f = open("a.txt", "wN")
。在Python 3中,默认情况下就是这样的。 - Eryk Sun