Advertisements
Advertisements
प्रश्न
Write the syntax for getopt() and explain its arguments and return values?
उत्तर
Syntax of getopt():
, =getopt.
getopt(argv,options, [long options])
where
i) argv:
This is the argument list of values to be parsed (split). In our program, the complete command will be passed as a list.
ii) options:
This is a string of option letters that the Python program recognizes as, for input or for output, with options (like or ‘o’) that followed by a colon (r), Here a colon is used to denote the mode.
iii) long_options:
This parameter is passed with a list of strings. The argument of Long options should be followed by an equal sign C=’). In our program, the C++ file name will be passed as a string and ‘I’ also will be passed along to indicate it as the input file.
- getopt( ) method returns value consisting of two elements.
- Each of these values is stored separately in two different lists (arrays) opts and args.
- opts contains a list of split strings like mode, path.
- args contains any string if at all not split because of wrong path or mode.
- args will be an empty array if there is no error in splitting strings by getopt().
Example:
opts, args = getopt. getopt
(argv, “i:”, [‘ifile=’])
where opts contains | [(‘-i’, /c:\\pyprg\\p4′)] |
-i:- | option nothing but mode should be followed by: |
c:\\pyprg\ \p4′ | value – the absolute path of the C++ file. , |
In our examples since the entire command line commands are parsed and no leftover argument, the second argument args will be empty [ ].
If args is displayed using print () command it displays the output as [].
APPEARS IN
संबंधित प्रश्न
The module which allows you to interface with the Windows operating system is ______
What is the use of modules?
Identify the module, operator, definition name for the following welcome.display()?
What is sys.argv? What does it contain?
What is the purpose of the sys, os, getopt module in Python? Explain.