Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

directory path with python argparse

import os
import argparse
from datetime import datetime


def parse_arguments():
    parser = argparse.ArgumentParser(description='Process command line arguments.')
    parser.add_argument('-path', type=dir_path)
    parser.add_argument('-e', '--yearly', nargs = '*', help='yearly date', type=date_year)
    parser.add_argument('-a', '--monthly', nargs = '*',help='monthly date', type=date_month)

    return parser.parse_args()


def dir_path(path):
    if os.path.isdir(path):
        return path
    else:
        raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")


def date_year(date):
    if not date:
        return

    try:
        return datetime.strptime(date, '%Y')
    except ValueError:
        raise argparse.ArgumentTypeError(f"Given Date({date}) not valid")


def date_month(date):
    if not date:
        return

    try:
        return datetime.strptime(date, '%Y/%m')
    except ValueError:
        raise argparse.ArgumentTypeError(f"Given Date({date}) not valid")


def main():
    parsed_args = parse_arguments()

if __name__ == "__main__":
main()
Comment

PREVIOUS NEXT
Code Example
Python :: python num2words installation 
Python :: how to remove duplicates from a python list 
Python :: python append filename to path 
Python :: python subprocess exception handling 
Python :: django pandas queryset 
Python :: slicing of tuple in python 
Python :: Python code for checking if a number is a prime number 
Python :: django start project 
Python :: python pyqt5 
Python :: contextlib.subppress python 
Python :: how call module in the same directory 
Python :: access django server from another machine 
Python :: execute terminal command from python 
Python :: Load dataset from seaborn 
Python :: django execute 
Python :: how to use function in python 
Python :: find data in sheet pandas 
Python :: round off to two decimal places python 
Python :: process rows of dataframe in parallel 
Python :: add column in spark dataframe 
Python :: add column to start of dataframe pandas 
Python :: create qr code in python 
Python :: swap variables in python 
Python :: matplotlib pyplot comment on plot 
Python :: python is inf 
Python :: NumPy unique Example Get the unique rows and columns 
Python :: check if file is txt python 
Python :: map and filter in python 
Python :: how to add a fuction in python 
Python :: python turtle jupyter notebook 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =