Reputation: 251
As in the code, there are two parser.add_argument --date and --type. If --type exists, I want to use another function. How can I check the existence of --type? I tried this but it failed
parser = argparse.ArgumentParser()
parser.add_argument('--date', default=str(startDate))
parser.add_argument('--type', required=False)
args = parser.parse_args()
if len(args.type) > 0:
dataFrame = spDataFrame[nospDataFrame['Type'] == args.type].reset_index(drop=True)
else:
campDataframe = sp_camp_mapping(startDate, endDate, keyword, driver, filePath)
sp_mapping(campDataframe, driver, startDate)
ERROR:None type has no len
Upvotes: 0
Views: 928
Reputation: 41
If --type
parameter is not passed then the key type
in args will have a value of None
and:
len(None)
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.1\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
TypeError: object of type 'NoneType' has no len()
What you can do is check the value of args.type
directly:
import argparse
startDate = '20210209'
parser = argparse.ArgumentParser()
parser.add_argument('--date', default=str(startDate))
parser.add_argument('--type', required=False)
args = parser.parse_args()
if args.type:
print("Type parameter was passed: {}".format(args.type))
else:
print("Type parameter wasn't passed: {}".format(args.type))
Note: if args.type
is equivalente to say if args.type is not None:
Upvotes: 1