Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6123589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:04+00:00 2026-05-23T16:02:04+00:00

I am a novice to Python and have started to write my first module

  • 0

I am a novice to Python and have started to write my first module which will perform a backup to external storage (typically one or more USB disks).

The desired behaviour is:

  1. check if destination (backup disk) is mounted already. destination_was_mounted becomes True or False
  2. If destination_was_mounted = False then mount the destination.
  3. If mountDestination fails, raise exception and restart loop.
  4. If mountDestination is ok, check if checkfile (and backup_dir) exists on destination.
  5. if check_destination fails raise exception and restart loop.
  6. Continue processing (have not coded this yet).
  7. Under any condition if destination_was_mounted = False then un-mount the destination.

The problem is that if the check_destination part raises an exception it fails to unmount the destination even though I have that in the finally section. It’s as if the destination_was_mounted becomes True even it was supposed to be False. Or as if check_destination runs before mount_destination even though it’s after it.

My references (amongst looking at the python documentation and my Learning Python book):

Python: How to tell the for loop to continue from a function?

How to retry after exception in python?

How to get back to the for loop after exception handling

#!/usr/bin/env python3.1

import sys
import os
import os.path
import subprocess
import configparser
CONFIGFILE = 'backup.ini'
# do i need this?
config = {}

config = configparser.ConfigParser()
config.read(CONFIGFILE)
backup_sources = sorted(config.sections()[1:])

class NoCheckFile(Exception):
    pass

def mountDestination(destination):
    return subprocess.check_call(['mount', destination])

def unMountDestination(destination):
    return subprocess.check_call(['umount', destination])

def checkDestination(destination, backup_dir, checkfile):
    return os.path.exists(destination + '/' + backup_dir + '/' + checkfile)

''' exception handlers '''
# perhaps add another arg like 0 or 1 for success/failure
def handleCalledProcessError(ex):
    print('Exception: ' + str(ex))

def handleNoCheckFile(ex):
    print('Exception: ' + str(ex))

# rename me once I work out logging
def logExecute(result):
    print('Info: ' + str(result))
# can I pass logging output here

def main():
    for section in backup_sources:
        item = dict(config.items(section))
        destination = item['destination']
        destination_was_mounted = os.path.ismount(destination)
        backup_dir = item['backup_dir']
        checkfile = item['checkfile']
        try:
            ''' check destination_was_mounted and mount destination if required '''
            mount_destination = None
            unmount_destination = None
            if not destination_was_mounted:
                mount_destination = mountDestination(destination)

            ''' check that checkfile exists in backup_dir '''
            check_destination = checkDestination(destination, backup_dir, checkfile)
            if not check_destination:
                raise NoCheckFile('no checkfile found')

            ''' lvm snapshot, mount and source path update '''

            ''' backup engine code here '''

            ''' check destination_was_mounted and um-mount destination if required '''
            if not destination_was_mounted:
                unmount_destination = unMountDestination(destination)
        except subprocess.CalledProcessError as ex:
            print(destination, 'mounted before loop start: ', destination_was_mounted)
            handleCalledProcessError(ex)
        except NoCheckFile as ex:
            handleNoCheckFile(ex)
        else:
            print(destination, 'mounted before loop start: ', destination_was_mounted)
            logExecute(mount_destination)
            logExecute(check_destination)
        finally:
            print('should always see me')
            logExecute(unmount_destination)
            # return to say True or False

    # this should be where email reports etc. go

if __name__ == '__main__':
    main()

The relevant parts of the backup.ini file are:

[general]

[1]
DESTINATION = /mnt/backup2
BACKUP_DIR = BACKUP2
CHECKFILE = .checkfile

[2]
DESTINATION = /mnt/backup1
BACKUP_DIR = BACKUP1
CHECKFILE = .checkfile

The output looks like this – I have 2 backup disks attached at the mount points specified in [1] and [2] and have intentionally not created the checkfile for [1] to test.

> umount /mnt/backup1
umount: /mnt/backup1: not mounted
> umount /mnt/backup2 
umount: /mnt/backup2: not mounted
> mugsyback.py 
Exception: no checkfile found
should always see me
Info: None
/mnt/backup1 mounted before loop start:  False
Info: 0
Info: True
should always see me
Info: 0
> mount
...
/dev/sdc1 on /mnt/backup2 type ext3 (rw)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:02:05+00:00Added an answer on May 23, 2026 at 4:02 pm

    You have the unmounting code in the try block. In finally, you’re just logging.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does python have some undefined variable / debug mode which will output a notice/warning?
I have just started to look at the Scala collections library re-implementation which is
I have a Python class containing a list, to which I append() values. If
In a Python Google App Engine environment, I have some highly volatile data which
I'm a Python novice, trying to use pyCurl. The project I am working on
As a novice developer who is getting into the rhythm of my first professional
I am a AS3 novice learning PureMVC and want to write code following best
I am novice in sharepoint programming. I have a following code: SPWorkflowTask task =
I am a novice in the world of source/version control and I have been
I'm a novice at programming although I've been teaching myself Python for about a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.