Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Jan 2010
    Location
    usa
    Posts
    234

    continue python 3 program running after except


    was told to use pass but the program just ends how do I keep it going?

    Code:
    import os
    import hashlib
    import sys
    
    start_path = '/home/lance' # current user home directory
    files_dir = []
    for path,dirs,files in os.walk(start_path):
        for filename in files:
            files_dir.append(os.path.join(path,filename))
                    
    def pwalk():
        try:
            for x in files_dir:
                hasher = hashlib.sha256()
                with open(x, 'rb') as openfile:
                    content = openfile.read()
                    hasher.update(content)
                    with open('data.txt', 'a') as f:
                        print(hasher.hexdigest(), x, sep=",", file=f)
        except PermissionError as permission_error:
            print(permission_error)
            pass
        except FileNotFoundError as filenotfounderror:
            pass
            print(filenotfounderror)
        input('Enter to close...')
    
    pwalk()

  2. #2
    Linux Newbie
    Join Date
    Jan 2010
    Location
    usa
    Posts
    234
    -->
    working code
    Code:
    #!/usr/bin/env python3
    import os
    import hashlib
    import sys
    
    start_path = input('Start path for file sha256sum\n') # current user directory set here
    files_dir = []
    for path,dirs,files in os.walk(start_path):
        for filename in files:
            files_dir.append(os.path.join(path,filename))
    
    # BUF_SIZE is totally arbitrary, change for your app!
    BUF_SIZE = 65536  # lets read stuff in 64kb chunks!                
    
    def pwalk():
        for x in files_dir:
            try:
                hasher = hashlib.sha256()
                with open(x, 'rb') as openfile:
                    content = openfile.read(BUF_SIZE)
                    hasher.update(content)
                    with open('data.csv', 'a') as f:
                        print(hasher.hexdigest(), x, sep=",")#, file=f)
                        f.write(hasher.hexdigest()+','+str(x)+'\n')
            except PermissionError as permission_error:
                print(permission_error)
                with open('errors.txt', 'a') as e:
                    e.write(str(permission_error)+'\n')
                pass
            except FileNotFoundError as filenotfounderror:
                pass
                print(filenotfounderror)
                with open('errors.txt', 'a') as e:
                    e.write(str(filenotfounderror)+'\n')
            except OSError as oserror:
                pass
                with open('errors.txt', 'a') as e:
                    e.write(str(oserror)+'\n')
                print(oserror)
        #input('Enter to close...')
    
    pwalk()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •