Python Forum
Handling exception from a module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handling exception from a module
#9
The if statement on line 23 is there because of the uncaught exceptions, I wanted to suppress the exception text and go straight to the else on line 26
import argparse, os, pdftitle, re

parser = argparse.ArgumentParser(description='Generate filenames from PDF titles.')
parser.add_argument('path', help='Starting folder path')
parser.add_argument('-r', '--rename', action='store_true', help='Rename files (otherwise just display)')
args = parser.parse_args()

def pdf_recurse(SrcFolder):

    for FileName in os.listdir(SrcFolder):
        FilePath = SrcFolder + '\\' + FileName
        if os.path.isdir(FilePath):
            pdf_recurse(FilePath)
        else:
            FileExt = FilePath[-3:]
            if FileExt.lower() == 'pdf':                
                PdfTitle = ""
                try:
                    PdfTitle = pdftitle.run(FilePath)
                except:                    
                    print(FilePath)
                    print("an exception occurred")
                if PdfTitle == "" or PdfTitle == 1:
                    print(FilePath)
                    print("Could not read")
                else:
                    NewName = new_name(PdfTitle, FileName)
                    if NewName != "" and NewName != FileName:
                        print(FilePath)
                        print(NewName)
                        if args.rename:
                            os.rename(r'' + str(FilePath), r'' + SrcFolder + '\\' + NewName)

def new_name(ReadTitle, FileName):
    if len(ReadTitle) < 6:
        return ""
    Match = re.search('(iptc|spe)[\s\-]{0,1}[0-9]+' , FileName, re.IGNORECASE)
    if Match != None:
        return ""
    if len(ReadTitle) > 72:
        ReadTitle = ReadTitle[:72]
    NewName = re.sub('[^\w_.)( -]', '', ReadTitle) + '.pdf'
    return NewName

pdf_recurse(args.path)
Reply


Messages In This Thread
Handling exception from a module - by dchi2 - Nov-23-2019, 09:18 AM
RE: Handling exception from a module - by Larz60+ - Nov-23-2019, 09:23 AM
RE: Handling exception from a module - by dchi2 - Nov-23-2019, 10:51 PM
RE: Handling exception from a module - by Larz60+ - Nov-23-2019, 11:12 PM
RE: Handling exception from a module - by dchi2 - Nov-23-2019, 11:35 PM
RE: Handling exception from a module - by Larz60+ - Nov-23-2019, 11:50 PM
RE: Handling exception from a module - by dchi2 - Nov-23-2019, 11:58 PM
RE: Handling exception from a module - by Larz60+ - Nov-24-2019, 03:32 AM
RE: Handling exception from a module - by dchi2 - Nov-24-2019, 05:23 AM
RE: Handling exception from a module - by Larz60+ - Nov-24-2019, 08:31 AM
RE: Handling exception from a module - by dchi2 - Nov-24-2019, 09:40 AM
RE: Handling exception from a module - by dchi2 - Nov-25-2019, 08:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,325 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,475 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Error handling using cmd module leifeng 3 2,946 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Exception handling in regex using python ShruthiLS 1 2,389 May-04-2020, 08:12 AM
Last Post: anbu23
  Exception handling Calli 2 2,498 Apr-20-2020, 06:13 PM
Last Post: Calli
  problem using custom exception handling in python srm 3 3,098 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  an easy way to disable exception handling Skaperen 6 5,529 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception handling KyawMyo 3 2,908 May-07-2019, 07:53 AM
Last Post: buran
  Database operation exception handling LostInCode 1 2,519 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain
  During handling of the above exception, another exception occurred Skaperen 7 26,993 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