Home > Enterprise >  Python accept predefined input along with any input inside of a user input and save as variable
Python accept predefined input along with any input inside of a user input and save as variable

Time:01-22

So I have a "terminal" like program written in python and in this program I need to accept "mkdir" and another input and save the input after mkdir as a variable. It would work how sys.argv works when executing a python program but this would have to work from inside the program and I have no idea how to make this work. Also, sorry for the amount of times I said "input" in the title, I wasn't sure how to ask this question.

user = 'user'
def cmd1():
    cmd = input(user   '#')
    while True:
        if cmd == 'mkdir '   sys.argv[1]:   #trying to accept second input here
            print('sys.argv[1]')
            break
        else:
            print('Input not valid')
            break

CodePudding user response:

Are you just trying to accept arguments that are entered after mkdir? If so, you could just split the string with whitespace and get any words after mkdir.

cmd = input(user   '#')
cmd_split = cmd.split(' ')

if cmd_split[0] == 'mkdir':
    args = cmd_split[1:]

CodePudding user response:

It looks like you're trying to tokenize your cmd. That is, you want to split out the parts of the input by their spaces.

cmd = input(user   '#')
parts = cmd.split()

# get the first part as the instruction
instruction = parts[0]

# get everything else as the args
args = parts[1:]
  •  Tags:  
  • Related