I need to parse .txt file as argument for my script. And then split it by lines, turn it into list and print it.
parser = argparse.ArgumentParser()
parser.add_argument('textA', type=argparse.FileType('r'), nargs=1, default='textA.txt')
args = parser.parse_args()
textA = args.textA.read().split('\n')
print(textA)
But ends up with AttributeError: 'list' object has no attribute 'read'
in console
I think I just don't know how to parse file properly
The narg
parameter is the cause of the problem.
This is the documentation that explains the usage of this parameter https://docs.python.org/3/library/argparse.html?highlight=argparse#nargs
nargs="?"
in this case. args.textA.read()
to args.textA[0].read()
and leave nargs=1
as is