简体   繁体   中英

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

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

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:]

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:]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM