有没有办法在Python中获取更具体的错误消息? E.g完整错误代码或至少发生错误的行,无法找到的确切文件而不是通用"The system cannot find the file specified")
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')
以下打印两次:
FileNotFoundError(2, 'The system cannot find the file specified')
虽然这很棒,但是有没有办法获得更准确的错误消息以进行错误修复?
以下内容会停止作业,但会在控制台中显示exact line being 855
以及文件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''
答案 0 :(得分:1)
请参阅traceback模块:
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'
输出:
// Create an instant object from the timestamp
Instant instant = Instant.ofEpochMilli(3600);
instant.atZone(ZoneOffset.UTC);
//Add X number of days to instant
instant.plus(1, ChronoUnit.DAYS);
// Create a date formatter
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
// Create the date from instant & format it from date formatter
Date date = Date.from(instant);
String formattedDate = formatter.format(date);
System.out.println(formattedDate);