2013-09-24 27 views
0

當我關閉文件對象時,得到的返回碼是2而不是「無」。Python中的返回代碼Fileobject.Close()

output = [] 
    cmd = cmd.lstrip() 
    if cmd.startswith('"') == True and cmd.count('"') > 2: 
     found = re.search(r'^".*?"', cmd) 
     if found != None: 
      shortName = win32api.GetShortPathName(found.group(0).replace('"','')) 
      cmd = cmd.replace(found.group(0), shortName) 

    rc = None 
    if debugOutput == False: 
     progOutput = os.popen(cmd) 
     line = progOutput.readline() 
     while (line) != "": 
      if capture == True: 
       output.append(self.chomp(line)) 
      if log == True: 
       print self.chomp(line) 
      line = progOutput.readline() 
     rc = progOutput.close() 
    else: 
     print "Would have executed: %s" % cmd 

    if rc == None: 
     rc = 0 
    if capture == True: 
     return rc, output 
    else: 
     return rc 

「RC」返回代碼是2,而不是在無。 有沒有人有一個想法是什麼在Python中的錯誤代碼?

回答

0

文檔爲os.popen

os.popen(命令[,模式[,BUFSIZE]])

...

該命令的退出狀態(編碼格式指定爲wait())可用作文件對象的close()方法的返回值,但退出狀態爲零(終止無錯誤)時,返回None

所以這個錯誤代碼來自操作系統(「文件未找到」,根據谷歌)。

+0

感謝您的回答。但文件存在那裏。這隻發生在特定的機器上。並非所有的機器。 Close()方法的返回碼是2而不是None。 –

+0

我真的不能爭辯,因爲我不知道你的情況是什麼''cmd''。但我傾向於相信文檔。如果它說''close''返回''cmd''的出口代碼,那麼''cmd''由於某種原因在這臺特定的機器上返回2。 – fjarri