简体   繁体   English

Python 接受预定义的输入以及用户输入中的任何输入并保存为变量

[英]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.所以我有一个类似“终端”的程序,用 python 编写,在这个程序中,我需要接受“mkdir”和另一个输入,并将 mkdir 之后的输入保存为变量。 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.在执行 python 程序时,它会起作用 sys.argv 的工作原理,但这必须在程序内部工作,我不知道如何使它工作。 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 .看起来您正在尝试标记您的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?您只是想接受在 mkdir 之后输入的 arguments 吗? If so, you could just split the string with whitespace and get any words after mkdir.如果是这样,您可以用空格分割字符串并在 mkdir 之后获取任何单词。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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