argparse and filetypes

Discussion in 'Python' started by Bradley Hintze, Mar 22, 2011.

  1. 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
     
    Bradley Hintze, Mar 22, 2011
    #1
    1. Advertising

  2. Bradley Hintze

    Alex Willmer Guest

    On Mar 22, 2:06 pm, Bradley Hintze <>
    wrote:
    > 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)


    Because you specified type=argparse.FileType('r') argparse has created
    args.infile as a file object (e.g. open('/some/path/data.dat', 'r')),
    not as a string containing the path. So type(args.infile) ==
    type(file) and args.infile.readlines() returns the contents of that
    file.

    If you wish to check the file extension of the path in question I
    suggest you remove type=argparse.FileType('r'), argparse will create
    args.infile as a string containing that path. To open the file call
    open(args.infile, 'r'), this will return the file object.
     
    Alex Willmer, Mar 22, 2011
    #2
    1. Advertising

  3. Bradley Hintze

    Alex Willmer Guest

    On Mar 22, 2:06 pm, Bradley Hintze <>
    wrote:
    > 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).


    Addendum, some file objects have a name attribute (which I hadn't
    noticed until today):

    file.name
    If the file object was created using open(), the name of the file.
    Otherwise, some string that indicates the source of the file object,
    of the form <...>. This is a read-only attribute and may not be
    present on all file-like objects.

    http://docs.python.org/library/stdtypes.html#file-objects
     
    Alex Willmer, Mar 22, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Straube
    Replies:
    2
    Views:
    1,167
  2. Replies:
    1
    Views:
    298
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Aug 25, 2005
  3. royG
    Replies:
    11
    Views:
    504
    Tim Chase
    Mar 11, 2008
  4. embirath

    askopenfilename filetypes problem

    embirath, Jul 2, 2010, in forum: Python
    Replies:
    0
    Views:
    1,327
    embirath
    Jul 2, 2010
  5. Brandon
    Replies:
    2
    Views:
    177
    Jevon
    Jan 18, 2006
Loading...

Share This Page