2017-11-05 2 views
1

Gibt es eine Möglichkeit, genauere Fehlermeldungen in Python zu bekommen? Beispiel: Die vollständige Fehlercode oder zumindest die Leitung trat der Fehler auf, die genaue Datei, die nicht eher gefunden werden kann als ein generisches "The system cannot find the file specified")Drucken präzise Fehlermeldungen in Python

for file in ['C:/AA/HA.csv', 'C:/AA1/HA1.csv']: 
     try: 
      os.remove(file) 
     except OSError as e: 
      pass 
      print(getattr(e, 'message', repr(e))) 
      #print(getattr(e, 'message', repr(e))) 
      #print(e.message) 
      #print('File Not Removed') 

Die folgenden druckt zweimal:

FileNotFoundError(2, 'The system cannot find the file specified') 

Während dieser groß ist Gibt es eine Möglichkeit, genauere Fehlermeldungen für die Fehlerbehebung zu bekommen?

Die folgende stoppt den Job, sondern gibt auch die exact line being 855 in der Konsole aus als die Datei directory ''C:/AC/HA.csv''.

os.remove('C:/AA/HA.csv') 
Traceback (most recent call last): 
    File "C:/ACA.py", line 855, in <module> 
    os.remove('C:/AC/HA.csv') 
FileNotFoundError: [WinError 2] The system cannot find the file specified: ''C:/AC/HA.csv'' 

Antwort

1

Siehe traceback Modul:

import os 
import traceback 

for file in ['C:/AA/HA.csv', 'C:/AA1/HA1.csv']: 
    try: 
     os.remove(file) 
    except OSError as e: 
     traceback.print_exc() 

Ausgang:

Traceback (most recent call last): 
    File "C:\test.py", line 6, in <module> 
    os.remove(file) 
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/AA/HA.csv' 
Traceback (most recent call last): 
    File "C:\test.py", line 6, in <module> 
    os.remove(file) 
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/AA1/HA1.csv'