How to get the error line?

I want to ask in general when I execute small code from the terminal I get the error but I dont know which line cause this error so how I know which line ?


Read the traceback. That will tell you where it detected the error.

The cause might be on that line or on an earlier line that it executed, but you just have to read the code leading up where it detected the error and think about what it’s doing to find the actual cause.

I dont see any traceback

Can you post the code you are trying to run? And the output you are seeing?

I cant post the code but the error I get

Error: ‘TSK_FS_INFO’ object has no attribute ‘meta’

Thats it no additional output. I am looking to learn in general how to know the cuased line

Normally there is a full traceback that shows the line. E.g. given this small script:

a = 10
b = 20
c = 30

the result from running in the terminal is:

bk333-py312 ❯ python
Traceback (most recent call last):
  File "/Users/bryan/tmp/", line 3, in <module>
ZeroDivisionError: division by zero

which shows the error was on line 3.

If you are not seeing a traceback then something is different about your code or your setup and we can’t help without more details and information.

1 Like

Is something in the code catching the exception, printing a message, and then quitting the program? That’s not helpful for debugging!

1 Like

yes this was the reason there is try … except block that print the error message. Thanks for this info I am still new to python

Is there a way to print caused line number inside except block ?

You could try adding:

import traceback

in the handler to print out the traceback.


Just don’t catch the exception. The very easiest way to print out errors, complete with tracebacks, is to do nothing at all.


This is a good example of why it’s generally (frequently? sometimes? always?) a bad idea to catch exceptions within modules meant to be called by other people’s code. Unless the module code is going to re-raise it as a module-specific exception or knows it’s innocuous and safe to discard, it’s generally better to let the calling code handle it.


Others have mentioned how you can print the current traceback, and thus
get line numbers, and that often the best thing to do is to not catch
the exception at all (which gets you a traceback when the exception
escapes all the way out).

You may be thinking: “I need to catch expections otherwise my program

The rough rule of thumb with exceptions is to catch them only when you
know what to do with them i.e. you know how to fix this particular

Here’s an example from my own code. It’s a bit dated (we tend to use
PermissionError these days) but it shows the pattern:

 except OSError as e:
   if e.errno == errno.EACCES:
     warning("save tags: %s", e)
     dst_taggedpath.modified = old_modified

Here I’m wanting to detect that I don’t have permission to save to a
particular file and when exactly that happens issue a warning message
and carry on. Otherwise I want to not catch the exception because it
is a different siutation and I do not have a plan for what to do with

So what’s salient in the above code?

 except OSError as e:

This catches just the OSError class of exceptions. I don’t have a plan
for any others, so I’m not going to catch any of those at all.

   if e.errno == errno.EACCES:

Here’s the particular flavour of OSError I understand: permission
denied. For this particular code, that’s ok, so I’m going to issue a
warning message which includes mention of the exception, and carry on.


And her’es the flip side: it isn’t the special circumstance I
understand and handle, so the raise statement reraises the exception,
letting it out.

The core idea here is that exceptions are just that: exceptional
circumstances which we do not expect to happen. You want them to get
out so that they can be debugged and the larger situation fixed. Maybe
the thing calling your code has done it wrong, maybe you’re running your
code against the wrong data file, etc etc. All of these are things which
are not your problems: the person/code calling your code has done so

Don’t catch (and thus hide) exceptions you’re not specificly

Cameron Simpson


The novice thinks it’s his job to stop the program from crashing. The expert knows that it’s his job to stop the Eagle from crashing. If you want to see what it’s like when you DON’T have really handy tracebacks, and people’s lives are on the line, look up the Apollo Guidance Computer and what happened during the Apollo 11 landing - they were getting information from their computer in the form of four-digit message codes.