argparse and filetypes

Bradley Hintze bradley.h at aggiemail.usu.edu
Tue Mar 22 10:06:41 EDT 2011


Hi,

I just started with argparse. I want to simply check the extension of
the file that the user passes to the program. I get a ''file' object
has no attribute 'rfind'' error when I use
os.path.splitext(args.infile).  Here is my code.

import argparse, sys, os

des = 'Get restraint definitions from probe.'
parser = argparse.ArgumentParser(description=des)
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'))
# parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
                   # default=sys.stdout)

args = parser.parse_args()
# print args.infile.readlines()
print basename, extension = os.path.splitext(args.infile)

There may be a better way to check extensions using argparse.

Any help will be appreciated.
Thanks,
Bradley

-- 
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine



More information about the Python-list mailing list