0

I'm trying to get the arguments of user input in python. I want to prompt the user to type something, and then store the words that they type in variables. I've tried doing 'sys.argv[1]' and 'cmd.argv[1]', but I just keep getting errors.

import sys
import os

print("Usage: .run <host> <time>")

cmd = input("[email protected]:~# ")

host = cmd.argv[2]
time = cmd.argv[3]

print("HOST: " + host)
print("TIME: " + time)
1
  • Is there a reason you are trying to make your program's prompt look like a shell prompt? That's a little misleading. Commented Aug 4, 2021 at 19:20

1 Answer 1

1

The return value of python's input (in 3.x.x) is a string. You'd use sys.argv if you were calling your code with the terminal, like

$ python mycode.py argument1 argument2

but as it stands, a string doesn't have .argv functionality.

The python split function might be helpful. If you need more complicated parsing, eg to enable 'argument 1' argument\ 2 being parsed into ['argument 1', 'argument 2'], you might find shlex a useful library (included with python)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.