0

I'm trying to parse the command-line argument in Python as follows:

python test.py /home/Desktop/test.xml

I've found getopt, but how does that work with three arguments? This source only shows 4, and I don't know how to have less. http://www.tutorialspoint.com/python/python_command_line_arguments.htm

And is DOM the most efficient way to parse XML files?

2 Answers 2

1

I would forgo getopt in favor of argparse, which has a very good tutorial here. argparse shows nicer errors with less work. I think getopt is used mainly for compatibility with the C API Both modules, however, support fewer arguments. In your particular case, you will probably want to do something like the following

import argparse

def parse_args():
    parser = argparse.ArgumentParser(usage)
    help = "The file to operate on"
    parser.add_argument("infile", type=argparse.FileType('r'), help=help)
    args = parser.parse_args()
    return args.infile

Depending on your needs, lxml can be very useful/powerful. I've personally used BeautifulSoup (for some not-so-complicated XML stuff). The recommended module in the Standard Library is ElementTree which has a nice API and can do a lot of XML things.

edit: getopt is not deprecated as I incorrectly stated before

Sign up to request clarification or add additional context in comments.

Comments

0

A single command-line argument is a simple case, no need for getopt or other parsing modules. Try using the last argument as a file name for use in any XML library.

For pythonic XML handling, use ElementTree .

import sys
import xml.etree.ElementTree as ET

if __name__ == '__main__':
    print 'args:', sys.argv
    print 'last arg:', sys.argv[-1]
    if len(sys.argv) <= 1:
        sys.exit()
    tree = ET.parse(sys.argv[-1])
    root = tree.getroot()
    for child in root:
        print child.tag, child.text

(Add file existence checks and error handling after basics are understood).

1 Comment

8 lines with argparse gives much more useful feedback to the user and does the "file existence checks and error handling" for free. If the script grows in complexity, expanding the CLI is also much simpler. With argv you're saving 3 lines of code and losing these benefits

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.