What is the output when the command-line argument python my_script.py input.txt output.txt is run on the terminal with import sys?

Prepare for the Computer Science (CS) III Exam. Study with multiple choice questions, detailed explanations, and comprehensive resources. Boost your confidence and ace the exam!

When executing a Python script from the command line with arguments, the sys module is commonly used to access those arguments. In this scenario, when the command python my_script.py input.txt output.txt is run, the Python interpreter will populate sys.argv, which is a list containing all the command-line arguments passed to the script.

The first element of sys.argv (at index 0) will always be the name of the script being run, which is my_script.py. The subsequent elements of the list will contain the additional arguments provided in the command line, in this case, input.txt and output.txt.

Thus, the full list stored in sys.argv will be:

  • my_script.py (the script name)

  • input.txt (the first argument)

  • output.txt (the second argument)

Therefore, the output of sys.argv will indeed be a list containing all three of these elements: ['my_script.py', 'input.txt', 'output.txt']. This confirms the selected choice is correct, as it accurately reflects the content of sys.argv after the execution of the script with the provided command-line arguments. The presence of additional numbers that

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy