Python : WindowsError: [Error 6] The handle is invalid

吃可爱长大的小学妹 提交于 2021-02-10 16:21:12

问题


I am trying to develop python plugin on QGIS and I am trying to execute binary programs using subprocess :

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

I get this problem:

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

I searched for others who have the same error, and they propose to invoke shell = True or use os.popen but it is not working.

For information, I am working on Windows 7 64 bits.


回答1:


Solved : I added shell = True

proc = subprocess.Popen(args,shell=True,stdout=open(outimploc, 'w'), stderr=open(outimplocerr,'w'),stdin = subprocess.PIPE, cwd=self.tranusConf.workingDirectory).communicate()



回答2:


I found a partial solution to my problem :

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

It works. but I am embarrased in my plugin with the multiple opens of windows cmd of the programs that are executed. This is not esthetically beautiful for my plugin.

Edit : the same with using stdin=subprocess.PIPE



来源:https://stackoverflow.com/questions/43000394/python-windowserror-error-6-the-handle-is-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!