-3

I have written a driver code to take file path from user and use that file path in my functions. Driver code is as below;

import sys
if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("Usage: %s input_file" % sys.argv[0])
        sys.exit()

    file_path = sys.argv[1]
    connection, color, numOf_Nodes,links = read_problem(file_path)

    print(links)
    graph_coloring(connection, color, 0, numOf_Nodes)

But when I run this code I got following error:

Usage: C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py input_file
An exception has occurred, use %tb to see the full traceback.

SystemExit

Shouldn't it ask for a file path from user? I am new to use a driver code therefore I might skip to write some parts for the driver code to work.

Any help?

svn_21
  • 17
  • 6

1 Answers1

0

The code is expecting that the user pass a file path on the command line. You want to execute your code with something like this:

python ipykernel_launcher.py /path/to/input/file

Upon execution of your code, the variable file_path will get a value of /path/to/input/file.

The variable sys.argv contains the script name in the first position, followed by each of the arguments passed to the script on the command line. That's why 2 is the appropriate expectation here...the first value in the array is the script name. The second value is the file path argument to the program.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • @SteveActually I will submit this code file (.py file) with a submit.pyc file via command prompt. However it could not submit to the system. Normally it should without giving the file path. Therefore I suspected that maybe I did sth wrong in the driver code part. – svn_21 Feb 08 '21 at 20:21
  • @svn_21 What are you submitting the code file to exactly? It sounds like we need more details to help you. – Code-Apprentice Feb 08 '21 at 20:23
  • @Code-Apprentice There is a website created to take our submissions via submit.pyc file. I have my own user name and password. I use this command from cmd: python submit.cpython-38.pyc 1 -u username -p password. In the working directory I have my data file, .py file and manifest.txt. So, normally by only above command it should take file and manifest and submit to the submission website – svn_21 Feb 08 '21 at 20:28
  • @svn_21 So is this for a university class assignment? It sounds like you need to ask your instructor about how to do what you want. – Code-Apprentice Feb 08 '21 at 20:29
  • @Code-Apprentice Yes it is, and I asked to my instructor, but she said that I should check my code :/ I checked my functions they are all working, so I suspected that I did sth wrong for the driver code part :/ – svn_21 Feb 08 '21 at 20:36
  • @svn_21 Yes, when code doesn't do what you want, it usually means there's something wrong with it, but there isn't enough information in your question for us to help you. – Code-Apprentice Feb 09 '21 at 06:24