This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
File entry lister script
Joachim Metz edited this page Feb 17, 2019
·
5 revisions
In this example we create a file entry lister script that can be pointed at a directory or an image file.
First let's start with the initial boiler plate:
- the shebang
- the encoding of the source file
- a description of what the script is supposed to do
- the main function skeleton
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Script to list file entries."""
from __future__ import print_function
from __future__ import unicode_literals
# TODO: add imports here.
import sys
# TODO: add classes here.
def Main():
"""The main program function.
Returns:
bool: True if successful or False if not.
"""
# TODO: add main program logic here.
return True
if __name__ == '__main__':
if not Main():
sys.exit(1)
else:
sys.exit(0)
For the program argument handling we'll use argparse.
Add the necessary import:
import argparse
And to the Main function add code that sets:
- the program description
- a program argument named source
argument_parser = argparse.ArgumentParser(description=(
'Lists file entries in a directory or storage media image.'))
argument_parser.add_argument(
'source', nargs='?', action='store', metavar='image.raw',
default=None, help=(
'path of the directory or filename of a storage media image '
'containing the file.'))
options = argument_parser.parse_args()
if not options.source:
print('Source value is missing.')
print('')
argument_parser.print_help()
print('')
return False
For notifying errors, warnings or debug information we'll use logging.
Add the necessary import:
import logging
And to the Main function add code that sets:
- the logging configuration
logging.basicConfig(
level=logging.INFO, format='[%(levelname)s] %(message)s')
TODO: complete section