What Is The Process Of This Program Written In Python?

- 1 answer

I have to understand the process of a program which looks like measuring the performance of multiplication for several repeat sizes.

I tried to execute the program, but there was no clue to understand it from the output. I need a help how to use this program and please let me know what are input and output of it.

import sys
import numpy as np
import matplotlib.pyplot as plt

if len(sys.argv) != 2:
    print ("usage:", sys.argv[0], "<filename>")

a = np.loadtxt(sys.argv[1])


list1, list2 = zip(*a)
plt.plot(list1, list2)

When I executed the above code, its output was below.

$ python
usage: <filename>

trial following the answer

I have executed the program like below. There was no change on the code of "". How can I fix the error and what is the appropriate content in fileToRead.txt?

$ python fileToRead.txt
Traceback (most recent call last):
  File "", line 24, in <module>
    list1, list2 = zip(*a)
TypeError: iteration over a 0-d array




It seems you are new to the world of programming. "sys.argv" is used to take Command Line Arguments.

  • when you run as "python", the variable sys.argv will be a single element list i.e. [""]
  • len(sys.argv) is 1 in this case

The Expected Working of the program is:

  • when you run as "python fileToRead.txt", the variable sys.argv will be a two element list i.e. ["","fileToRead.txt"]
  • len(sys.argv) is 2 in this case