I use argparse to get a file from the user:
import argparse, os
parser = argparse.ArgumentParser()
parser.add_argument('file', type=file)
args = parser.parse_args()
Then I want to know the directory where this file is, something like:
print(os.path.abspath(os.path.dirname(args.inputfile)))
But of course, as args.inputfile is a file object, this does not work. How to do it?
You can get the name of the file from the
.nameattribute, and then pass this toos.path.abspath. For example: