What is the process of this program written in Python?

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>")
    exit()

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

print(a)

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

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

$ python sample.py
usage: sample.py <filename>

trial following the answer

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

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

fileToRead.txt

3.1415926535

Answer

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

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

The Expected Working of the program is:

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

Leave a Reply

Your email address will not be published. Required fields are marked *