Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中打印精确的错误消息_Python_Python 3.x_Error Handling_Subprocess - Fatal编程技术网

在Python中打印精确的错误消息

在Python中打印精确的错误消息,python,python-3.x,error-handling,subprocess,Python,Python 3.x,Error Handling,Subprocess,有没有办法在Python中获取更具体的错误消息?例如,完整的错误代码或至少是发生错误的那一行,找不到的确切文件,而不是一般的“系统找不到指定的文件”) 以下打印两次: FileNotFoundError(2, 'The system cannot find the file specified') 虽然这很好,但有没有一种方法可以获得更精确的错误消息来修复bug 以下命令停止作业,但在控制台中给出了确切的行855以及文件目录“C:/AC/HA.csv”。 os.remove('C:/AA/HA

有没有办法在Python中获取更具体的错误消息?例如,完整的错误代码或至少是发生错误的那一行,找不到的确切文件,而不是一般的
“系统找不到指定的文件”)

以下打印两次:

FileNotFoundError(2, 'The system cannot find the file specified')
虽然这很好,但有没有一种方法可以获得更精确的错误消息来修复bug

以下命令停止作业,但在控制台中给出了
确切的行855
以及文件
目录“C:/AC/HA.csv”。

os.remove('C:/AA/HA.csv'))
回溯(最近一次呼叫最后一次):
文件“C:/ACA.py”,第855行,在
删除操作系统('C:/AC/HA.csv')
FileNotFoundError:[WinError 2]系统找不到指定的文件:“C:/AC/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:/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'
回溯(最近一次呼叫最后一次):
文件“C:\test.py”,第6行,在
删除(文件)
FileNotFoundError:[WinError 3]系统找不到指定的路径:“C:/AA/HA.csv”
回溯(最近一次呼叫最后一次):
文件“C:\test.py”,第6行,在
删除(文件)
FileNotFoundError:[WinError 3]系统找不到指定的路径:“C:/AA1/HA1.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'' 
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()
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'