Python Forum
Handling exception from a module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handling exception from a module
#11
Thank you for your continued assistance. I see now that the whole issue is due to me stupidly editing the module. I commented out a section of the run() function in pdftitle.py as I could not otherwise work out how to pass a file name to it.

This was on a previous night while I was somewhat intoxicated...

Now however, if I go to the original pdftitle.py, I cannot determine how to pass the argument correctly?

PdfTitle = pdftitle.run(pdf=FilePath)
def run():
    try:
        parser = argparse.ArgumentParser(
            prog='pdftitle',
            description='Extracts the title of a PDF article',
            epilog='')
        parser.add_argument('-p', '--pdf',
                            help='pdf file', required=True)
        parser.add_argument('--replace-missing-char',
                            help='replace missing char with the one specified')
        parser.add_argument('-v', '--verbose',
                            action='store_true',
                            help='enable verbose logging')
        args = parser.parse_args()
        global VERBOSE, MISSING_CHAR
        VERBOSE = args.verbose
        MISSING_CHAR = args.replace_missing_char
        title = get_title_from_file(args.pdf)
        if title is None:
            return 1
        else:
            print(title)
            return 0
    except Exception as e:
        if VERBOSE:
            traceback.print_exc()
        return 1


if __name__ == '__main__':
    sys.exit(run())
Error:
Traceback (most recent call last): File "rename_post.py", line 40, in <module> pdf_recurse(args.path) File "rename_post.py", line 19, in pdf_recurse PdfTitle = pdftitle.run(pdf=FilePath) TypeError: run() got an unexpected keyword argument 'pdf'
Very sorry for the false path.
Reply
#12
For completeness, now that I have what I want from the script, but don't quite understand how I broke the exception throwing, here is the original run function from pdftitle.py:
def run():
    try:
        parser = argparse.ArgumentParser(
            prog='pdftitle',
            description='Extracts the title of a PDF article',
            epilog='')
        parser.add_argument('-p', '--pdf',
                            help='pdf file', required=True)
        parser.add_argument('--replace-missing-char',
                            help='replace missing char with the one specified')
        parser.add_argument('-v', '--verbose',
                            action='store_true',
                            help='enable verbose logging')
        args = parser.parse_args()
        global VERBOSE, MISSING_CHAR
        VERBOSE = args.verbose
        MISSING_CHAR = args.replace_missing_char
        title = get_title_from_file(args.pdf)
        if title is None:
            return 1
        else:
            print(title)
            return 0
    except Exception as e:
        if VERBOSE:
            traceback.print_exc()
        return 1
Here is the edit I made to call the run function without it using an ArgumentParser (added "pdf" argument to run definition line 1, commented out ArgParser section, changed argument for get_title_from_file line 20 from args.pdf to just pdf.

Obviously I didn't expect this to break exception throwing as it did, if anyone is able to enlighten me on what went wrong, I'd like to learn from the mistake.

def run(pdf):
    try:
'''
        parser = argparse.ArgumentParser(
            prog='pdftitle',
            description='Extracts the title of a PDF article',
            epilog='')
        parser.add_argument('-p', '--pdf',
                            help='pdf file', required=True)
        parser.add_argument('--replace-missing-char',
                            help='replace missing char with the one specified')
        parser.add_argument('-v', '--verbose',
                            action='store_true',
                            help='enable verbose logging')
        args = parser.parse_args()
'''
        global VERBOSE, MISSING_CHAR
        VERBOSE = args.verbose
        MISSING_CHAR = args.replace_missing_char
        title = get_title_from_file(pdf)
        if title is None:
            return 1
        else:
            print(title)
            return 0
    except Exception as e:
        if VERBOSE:
            traceback.print_exc()
        return 1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,215 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,376 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Error handling using cmd module leifeng 3 2,801 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Exception handling in regex using python ShruthiLS 1 2,328 May-04-2020, 08:12 AM
Last Post: anbu23
  Exception handling Calli 2 2,409 Apr-20-2020, 06:13 PM
Last Post: Calli
  problem using custom exception handling in python srm 3 2,995 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  an easy way to disable exception handling Skaperen 6 5,297 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception handling KyawMyo 3 2,780 May-07-2019, 07:53 AM
Last Post: buran
  Database operation exception handling LostInCode 1 2,448 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain
  During handling of the above exception, another exception occurred Skaperen 7 26,723 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020