📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 268question

Refer installation of MinGW in Annexure -2 · Part 7

Chapter 4: 6 · COMPUTER SCIENCE

c:\pyprg\pali.py -i c:\pyprg\ import sys, os, getopt def main(argv): opts, args = getopt.getopt(argv, "i:") for o, a in opts: if o in "-i": run(a) def run(a): =a+'.cpp' =a+'.exe' os.system('g++ ' + + ' -o ' + ) os.system( ) if ==' ': main(sys.argv[ :]) Output of the above program Output C:\Users\Dell>python c:\pyprg\pali.py -i c:\pyprg\ Enter a positive number: 56765 The reverse of the number is: 56765 The number is a palindrome Output C:\Users\Dell>python c:\pyprg\pali.py -i c:\pyprg\ Enter a positive number: 56756 The reverse of the number is: 65765 The number is not a palindrome 12th Computer Chapter - - Importing C++ Programs in Python Python code How does it works import sys, os, getopt include sys , os and getopt modules to use the required function def main(argv): Function main() is defined and 'argv' contains the 'input mode and the c++ program file' in the form of list i.e ['-i', 'c:\ pyprg \ '] opts, args = getopt.getopt(argv, "i:") getopt() splits the command as option and argument. ‘opts’ contains [('-i', 'c:\pyprg\ ')]. Since no error ‘args’ shows [] for o, a in opts: ‘o’ contains the mode and ‘a’ contains the path of c++ program i.e print("o = ",o) shows o = -i print("a = ",a) shows a = c:\pyprg\ if o in ("-i"): Checks o == ‘i’ if true run(a) Calls the function run() passed along with the c++ program def run(a): Definition of run() function begins here =a+'.cpp' Variable ‘ ’ contains the joined c++ program name and .cpp print( ) shows c:\pyprg\ .cpp =a+'.exe' Variable ’ ’ contains the joined c++ program name and .exe print( ) shows c:\pyprg\ .exe os.system('g++ ' + + ' -o ' + ) g++ compiler compiles the c++ program in and store the executable file in os.system( ) Executes the exe file if ==' ': stores name of the python program __ also stores the name of the python program. main(sys.argv[ :]) If 'name' and 'main' are equal then main() is called and passed with the command line argument omitting the python program name argv[ :] contains –i c:\pyprg\ 12th Computer

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →