Does Python have an argc argument?

116,466

Solution 1

In python a list knows its length, so you can just do len(sys.argv) to get the number of elements in argv.

Solution 2

I often use a quick-n-dirty trick to read a fixed number of arguments from the command-line:

[filename] = sys.argv[1:]

in_file = open(filename)   # Don't need the "r"

This will assign the one argument to filename and raise an exception if there isn't exactly one argument.

Solution 3

You're better off looking at argparse for argument parsing.

http://docs.python.org/dev/library/argparse.html

Just makes it easy, no need to do the heavy lifting yourself.

Share:
116,466

Related videos on Youtube

Dan1676
Author by

Dan1676

Currently a 3rd year Ethical Hacking student at University of Abertay Dundee. Use mainly C#, Perl, Python. Started working in the Android Environment

Updated on April 02, 2020

Comments

  • Dan1676
    Dan1676 about 4 years

    I have written the same program (open text file and display contents) in C and C++. Now am doing the same in Python (on a Linux machine).

    In the C programs I used the code:

    if (argc != 2) {
        /* exit program */
    }
    

    Question: What is used in Python to check the number of arguments

    #!/usr/bin/python
    import sys
    try:
        in_file = open(sys.argv[1], "r")
    except:
        sys.exit("ERROR. Did you make a mistake in the spelling")
    text = in_file.read()
    print text
    in_file.close()
    

    Current output:

    ./python names.txt = Displays text file (correct)
    ./python nam = error message: stated from the sys.ext line (correct)
    ./python = error message: stated from the sys.ext line (wrong: want it to be a
    separate error message stating *no file name input*)
    
    • jww
      jww about 5 years
      Possible duplicate of python and sys.argv
    • ThorSummoner
      ThorSummoner over 4 years
      From a object orientation perspective, I can understand why argc should be coupled to argv. If these were two objects, they could be mutated interdependently of each other. It makes sense for argc to be an attribute of argv (and not of sys), I just am not sure len is the defacto attribute name i would use. argv.__len__ is critical for mechanical reasons, and since its there it doesn't make a ton of sense to give it another name. I would probably have favored a sys proxy descriptor like sys.argc = property(lambda self: len(self.argv)).__get__(sys, sys.__class__) though
  • Teo
    Teo over 4 years
    import sys # count the arguments arguments = len(sys.argv) - 1 print ("the script is called with %i arguments" % (arguments))
  • DoomGoober
    DoomGoober almost 4 years
    I like "better to ask forgiveness than permission" for most cases in Python. In the case of command line args though, an arguably better user experience would be to show the command line arguments that are required for the Python script if the wrong number of arguments are present.
  • Cees Timmerman
    Cees Timmerman almost 4 years
    @DoomGoober "supplied filename" is the argument it expects.