Skip to main content
deleted 3 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

str is already the default type of any variable parsed with argparse. Also the default metavar is just the name itself.

The cool thing about argparse is that it allows you to use custom variable parsing functions as type. So you'llyou could put the file type (or actually only file ending) check there:

import os


def csv_file(filepath):
    if not filepath.endswith('.csv'):
        raise ValueError("Input file must be a .csv file.")
    if not os.path.isfile(filepath):
        raise FileNotFoundError("Could not find file {}".Formatformat(filepath)
    return filepath


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("filepath", type=csv_file, help="Filepath to the input csv file.")
    return vars(parser.parse_args())

If an exception is raised in the type function, argparse will catch it, display it and exit the program.

str is already the default type of any variable parsed with argparse. Also the default metavar is just the name itself.

The cool thing about argparse is that it allows you to use custom variable parsing functions as type. So you'll could put the file type (or actually only file ending) check there:

import os


def csv_file(filepath):
    if not filepath.endswith('.csv'):
        raise ValueError("Input file must be a .csv file.")
    if not os.path.isfile(filepath):
        raise FileNotFoundError("Could not find file {}".Format(filepath)
    return filepath


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("filepath", type=csv_file, help="Filepath to the input csv file.")
    return vars(parser.parse_args())

If an exception is raised in the type function, argparse will catch it, display it and exit the program.

str is already the default type of any variable parsed with argparse. Also the default metavar is just the name itself.

The cool thing about argparse is that it allows you to use custom variable parsing functions as type. So you could put the file type (or actually only file ending) check there:

import os


def csv_file(filepath):
    if not filepath.endswith('.csv'):
        raise ValueError("Input file must be a .csv file.")
    if not os.path.isfile(filepath):
        raise FileNotFoundError("Could not find file {}".format(filepath)
    return filepath


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("filepath", type=csv_file, help="Filepath to the input csv file.")
    return vars(parser.parse_args())

If an exception is raised in the type function, argparse will catch it, display it and exit the program.

Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

str is already the default type of any variable parsed with argparse. Also the default metavar is just the name itself.

The cool thing about argparse is that it allows you to use custom variable parsing functions as type. So you'll could put the file type (or actually only file ending) check there:

import os


def csv_file(filepath):
    if not filepath.endswith('.csv'):
        raise ValueError("Input file must be a .csv file.")
    if not os.path.isfile(filepath):
        raise FileNotFoundError("Could not find file {}".Format(filepath)
    return filepath


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("filepath", type=csv_file, help="Filepath to the input csv file.")
    return vars(parser.parse_args())

If an exception is raised in the type function, argparse will catch it, display it and exit the program.