Python:WindowsError:[错误6]句柄无效

时间:2017-03-24 13:11:30

标签: python subprocess

我正在尝试在QGIS上开发python插件,我正在尝试使用子进程执行二进制程序:

program = os.path.join(self.tranusConf.tranusBinPath,'pasos' + self.extension)
    if not os.path.isfile(program):
        logging.error('The <pasos> program was not found in %s'%self.tranusBinPath )
        return 0
    outpasos = os.path.join(self.resultDirectory, "outpasos.txt")
    outpasoserr = os.path.join(self.resultDirectory, "outpasoserr.txt")
    args = [program, self.tranusConf.scenarioId, " "]
    result = subprocess.Popen(args,stdout=open(outpasos, "w"), stderr = open(outpasoserr, 'w'), close_fds = False, cwd = self.tranusConf.workingDirectory) # Success! 
    return 1

我遇到了这个问题:

An error has occurred while executing Python code: 
     WindowsError: [Error 6] Descripteur non valide  Traceback (most recent call last): File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\launch_tranus_dialog.py", line 109, in run_tranus
            interface.runTranus(tab.spin_box.value())
          File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\LcalInterface.py", line 426, in runTranus
            self.runPasos()
          File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\LcalInterface.py", line 311, in runPasos
            result = subprocess.Popen(args,stdout=open(outpasos, "w"), stderr = open(outpasoserr, 'w'), close_fds = False, cwd = self.tranusConf.workingDirectory) # Success!
          File "C:\OSGEO4~1\apps\Python27\lib\subprocess.py", line 703, in __init__
            errread, errwrite) = self._get_handles(stdin, stdout, stderr)
          File "C:\OSGEO4~1\apps\Python27\lib\subprocess.py", line 839, in _get_handles
            p2cread = self._make_inheritable(p2cread)
          File "C:\OSGEO4~1\apps\Python27\lib\subprocess.py", line 878, in _make_inheritable
        _subprocess.DUPLICATE_SAME_ACCESS)
    WindowsError: [Error 6] Descripteur non valide

我搜索了其他具有相同错误的人,他们建议调用shell = True或使用os.popen但它无效。

有关信息,我正在使用Windows 7 64位。

2 个答案:

答案 0 :(得分:1)

解决:我添加了shell = True

select sv2.UserId, min(sv2.EntryDate) as first
, min(if(date(sv2.EntryDate)=mindate,'2050-01-01', sv2.EntryDate)) as second
from motif_segmentvalue sv2
join (
    select sv3.UserId, min(date(sv3.EntryDate)) as mindate 
    FROM motif_segmentvalue sv3
    WHERE sv3.UserId = "5407"
) as temp ON temp.UserId = sv2.UserId
WHERE sv2.UserId = "5407"

答案 1 :(得分:0)

我找到了解决问题的部分方法:

devnull = open(os.devnull, 'wb')
result = subprocess.Popen(args,stdout = open(outtrans, "w"), stderr = open(outtranserr,'w'),stdin=devnull, cwd = self.tranusConf.workingDirectory).communicate()

有效。但我在我的插件中尴尬,多次打开执行的程序的Windows cmd。这对我的插件来说并不美观。

修改:与使用stdin=subprocess.PIPE

相同
相关问题